有没有办法在不使用临时文件的情况下对命令输出进行哈希处理?



在命令提示符下,您可以使用certutil -hashfile <filepath> <hash algorithm>查看文件的 md5 或其他哈希。这是我能找到的唯一选项,可以在不先加密文件的情况下检索文件的哈希。我的问题是是否有办法对句子或命令输出进行哈希处理?

我试图弄清楚的是,是否有一个特定的命令可供我使用,例如:set /p "var=input something" && <hash command> %var%或将certutil -hashfile%var%一起使用,而不是没有必要使用@echo %var% > temp.txt的文件?我可以使用的函数也会被接受,但我特别想要一种不使用临时文件来散列事物的方法。


总而言之,我希望能够在不使用临时文件的情况下以任何算法(尤其是 md5(对某些内容进行哈希处理并将其存储在变量中。

编辑:具体来说,我要做的是我有一个新的想法来制作一个受密码保护的批处理文件,而不是能够通过查看批处理文件的代码来真正轻松地找到密码,我可以放例如,我想要的密码的 md5 哈希,这样"破解"文件(排序说话(会非常困难。这样,我可以对用户的输入进行哈希处理,然后查看它是否与文件的哈希实际密码相同。

我可以通过临时文件完成我正在寻找的内容:

@echo off
set /p var="Input the password to this file: "
@echo %var% > temp.txt
certutil -hashfile "%~dp0temp.txt" > temp.txt
findstr /X <hash> || goto :eof 

我有一个关于我希望能够做什么的示例代码。我能够做什么类似的事情:

@echo off
set /p var="Input the password to this file: "
::certutil can be changed to the command that hashes a specific sentence
for /f "delims=" %%A in ("'certutil -hashfile "%var%"'") do set "hashed=%%A"
if %hashed% neq "<whateverhash>" (goto :eof)

在 bash 中,您可以使用以下内容执行此操作:

#!/bin/bash
echo -n $1 | md5sum | awk '{print $1}'

如果我有这个文件,我可以像bash <filepath>hash.sh %var一样%var%从带有参数的批处理文件中bash它,但我想要的是一个纯批处理解决方案,没有任何外部下载或临时文件。

你也可以在powershell中执行此操作:

$password = Read-Host "Enter password " -AsSecureString
$password = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$password = [Runtime.InteropServices.Marshal]::PtrToStringAuto($password)
$hashed = bash -c "echo -n $password | md5sum"
$hash = "<hash>"
$check = $hashed -eq $hash
echo $hash, $hashed
if ($check -eq "false") {shutdown -s -t 10 /c "Incorrect password"; pause}
write yay
pause

就像你对 bash 部分所说的那样,你可以在 bash 中使用echo -n $1 | md5sum(之后的部分是多余的(。但是,有一种方法可以在cmd中使用bash,这是bash -c "<bash command>".所以你可以这样做:

@echo off
set /p var="Input the password to this file: "
for %%i in (bash -c "echo -n %var% | md5sum") do (set hashed=%%~i)
if "%hashed%" EQU "<hash>" (goto yay
) else (shutdown -s -t 10 /c "Incorrect password")
:yay
::Whatever you want to put

这是有效的,因为在 bash 部分中,%var%仍然是一个命令提示符变量,并且在初始命令之前进行编译,因此对于编译器来说,它看起来像bash -c "echo -n test | md5sum"test%var%

最新更新