batch-file - 将文本转换为 md5sum 值的批处理文件

标签 batch-file md5sum

我需要创建一个具有以下条件的 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();
}

关于batch-file - 将文本转换为 md5sum 值的批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35178341/

相关文章:

batch-file - 用于分配变量并与字符串进行比较的批处理文件

python - 如何在 python 中执行批处理文件后关闭命令提示符窗口?

batch-file - 使用 Windows 从 ftp 下载时是否有解决 ":"(冒号)的方法

bash - md5sum 仅输出文件中的散列

c - 使用 md5sum 更简单的不变量

windows - 如何在 Windows 批处理脚本中获取可选的命令行参数值作为字符串?

windows - 如何使 Windows START 命令通过管道接受标准输入并将其传递给它调用的命令?

linux - 比较两个服务器之间的文件校验和并报告不匹配

c - c中int的md5sum数组

linux - 为什么符号链接(symbolic link)的md5校验和等于原始文件