将文本转换为MD5校验值的批处理文件

3

我需要创建一个符合以下条件的Windows“批处理”文件: 将文本转换为md5sum值。 我尝试了以下.bat文件。但不起作用。

@echo off

setlocal ENABLEDELAYEDEXPANSION

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

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

1
你可能会发现这个答案很有用。 - rojo
2个回答

3
这里有另一种方法,同样使用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
不错,谢谢。只是要注意,为了让你的第一个示例工作,我必须进行两个小改变:
  1. 第11行:echo Example: set /P "=password" ^<NUL ^| %~nx0
  2. 第23行:certutil -hashfile "%file%" MD5 (将MD5大写)
- Eric G

1
检查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库不同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接