【问题标题】:Is there a way to hash a command output without the use of a temp file?有没有办法在不使用临时文件的情况下散列命令输出?
【发布时间】:2022-05-02 07:13:12
【问题描述】:

在命令提示符中,您可以使用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 "%~dp0\temp.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 它,参数为 %var% 就像 bash &lt;filepath&gt;\hash.sh %var 但我想要的是一个纯批处理解决方案,没有任何外部下载或临时文件。

【问题讨论】:

  • 你想用这个具体完成什么?这可能会使尝试找到解决方案变得更容易
  • 感谢您的提问,我在问题中添加了更多信息。

标签: batch-file cmd hash


【解决方案1】:

您也可以在 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

【讨论】:

    【解决方案2】:

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

    @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%

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 2021-10-15
      • 1970-01-01
      • 2019-10-28
      • 2020-09-12
      • 1970-01-01
      • 2018-04-02
      相关资源
      最近更新 更多