【问题标题】:Batch file to convert text to md5sum value将文本转换为 md5sum 值的批处理文件
【发布时间】:2016-02-03 13:25:32
【问题描述】:

我需要创建一个具有以下条件的 Windows“批处理”文件。 将文本转换为 md5sum 值 我尝试使用下面的 .bat 文件。但不工作

@echo off

setlocal ENABLEDELAYEDEXPANSION

set hashmd5=$(echo -n welcome1 | md5sum | cut -c 1-32);

printpass=$printpass"#####MD5: echo.%%hashmd5 "\n"

【问题讨论】:

标签: batch-file md5sum


【解决方案1】:

这是另一个版本,同样使用certutil 生成MD5 哈希。它将为您提供文件或字符串参数的 MD5sum,并且可以通过管道接受一行输入。此解决方案不依赖于随附的帮助脚本。

@echo off & setlocal

set "plaintext=%~1"
set "file=%temp%\%~n0.tmp"
set md5=

if "%~1"=="/?" (
    echo Usage: %~nx0 file^|string
    echo    or: command ^| %~nx0
    echo;
    echo Example: set /P "=password" ^<NUL ^| %~nx0
    goto :EOF
)

if not defined plaintext set /P "plaintext="

if exist "%plaintext%" (
    set "file=%plaintext%"
) else for %%I in ("%file%") do if %%~zI equ 0 (
    <NUL >"%file%" set /P "=%plaintext%"
)

for /f "skip=1 delims=" %%I in ('certutil -hashfile "%file%" MD5') do (
    if not defined md5 set "md5=%%I"
)

2>NUL del "%temp%\%~n0.tmp"

echo %md5: =%

仅仅因为我喜欢挑战,这里还有另一个版本,它可以通过管道接受多行,以及来自文件重定向的输入。 (当缓冲区为空时,阻止StdIn.AtEndOfStream 提示键盘输入并不容易。)

@if (@CodeSection == @Batch) @then
@echo off & setlocal & goto main

:usage
echo Usage: %~nx0 file^|string
echo    or: command ^| %~nx0
echo    or: %~nx0 ^< file
echo;
echo Example: set /P "=password" ^<NUL ^| %~nx0
goto :EOF

:main
set "plaintext=%~1"
set "file=%temp%\%~n0.tmp"
set md5=

rem // check for input via the pipeline; timeout nearly instantly if none
start /b "" cscript /nologo /e:JScript "%~f0" "%file%" watcher
cscript /nologo /e:JScript "%~f0" "%file%"

if "%~1"=="" for %%I in ("%file%") do if %%~zI equ 0 goto usage
if "%~1"=="/?" goto usage

if not defined plaintext set /P "plaintext="

if exist "%plaintext%" (
    set "file=%plaintext%"
) else for %%I in ("%file%") do if %%~zI equ 0 (
    <NUL >"%file%" set /P "=%plaintext%"
)

for /f "skip=1 delims=" %%I in ('certutil -hashfile "%file%" MD5') do (
    if not defined md5 set "md5=%%I"
)

2>NUL del "%temp%\%~n0.tmp"

echo %md5: =%
goto :EOF

@end // end Batch / begin JScript hybrid code

var fso = WSH.CreateObject('scripting.filesystemobject');
if (WSH.Arguments.Length > 1) {
    WSH.Sleep(1);
    if (!fso.FileExists(WSH.Arguments(0)))
        WSH.CreateObject('WScript.Shell').Exec('taskkill /im "cscript.exe" /f');
} else if (!WSH.StdIn.AtEndOfStream) {
    var file = fso.CreateTextFile(WSH.Arguments(0), 1);
    file.Write(WSH.StdIn.ReadAll());
    file.Close();
}

【讨论】:

  • 很好,谢谢。请注意,为了让您的第一个示例正常工作,我必须进行两个小改动:1. 第 11 行:echo Example: set /P "=password" ^&lt;NUL ^| %~nx0 2. 第 23 行:certutil -hashfile "%file%" MD5(大写 MD5)
【解决方案2】:

查看MD5.bat:

@echo off 
set "string=The quick brown fox jumps over the lazy dog"
(break|set/p=%string%)>~
call md5 ~ m5sting
echo %m5sting%
exit /b 0

尽管无论我做什么,我都无法获得与某些将字符串转换为 MD5 的在线工具相同的结果(但它们通常彼此不同)。这将始终在字符串末尾放置一个 EOF 字符,因为它会转换文件而不是纯字符串。

目前,上面的脚本给出的结果与(见 rojo 的评论)相同 powershell "[Security.Cryptography.HashAlgorithm]::Create('MD5').ComputeHash([Text.Encoding]::UTF8.GetBytes('%string%')) | %%{write-host -n $_.tostring('x2')}" 虽然仍然不同于 PHP 和一些 Javascript 库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-10
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    相关资源
    最近更新 更多