我需要创建一个符合以下条件的Windows“批处理”文件: 将文本转换为md5sum值。 我尝试了以下.bat文件。但不起作用。
@echo off
setlocal ENABLEDELAYEDEXPANSION
set hashmd5=$(echo -n welcome1 | md5sum | cut -c 1-32);
printpass=$printpass"#####MD5: echo.%%hashmd5 "\n"
我需要创建一个符合以下条件的Windows“批处理”文件: 将文本转换为md5sum值。 我尝试了以下.bat文件。但不起作用。
@echo off
setlocal ENABLEDELAYEDEXPANSION
set hashmd5=$(echo -n welcome1 | md5sum | cut -c 1-32);
printpass=$printpass"#####MD5: echo.%%hashmd5 "\n"
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();
}
echo Example: set /P "=password" ^<NUL ^| %~nx0
certutil -hashfile "%file%" MD5
(将MD5大写)
@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
powershell "[Security.Cryptography.HashAlgorithm] :: Create('MD5').ComputeHash([Text.Encoding] :: UTF8.GetBytes('%string%')) | %% {write-host -n $_.tostring('x2')}"
,但仍然与PHP和一些Javascript库不同。