有没有一种方法可以在不使用临时文件的情况下对命令输出进行哈希处理? [英] Is there a way to hash a command output without the use of a temp file?

查看:99
本文介绍了有没有一种方法可以在不使用临时文件的情况下对命令输出进行哈希处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在命令提示符下,您可以使用certutil -hashfile <filepath> <hash algorithm>查看md5或文件的其他哈希.这是我无需重新加密即可检索文件哈希值的唯一选择.我的问题是是否有一种对句子或命令输出进行哈希处理的方法?

In command-prompt, you can see the md5 or other hash of a file using certutil -hashfile <filepath> <hash algorithm>. This was the only option I can find to retrieving the hash of a file without encrypting it first. My question is if there is a way to hash a sentence or command outputs?

我要弄清楚的是,是否存在可以在以下情况下使用的特定命令:set /p "var=input something" && <hash command> %var%或将certutil -hashfile%var%一起使用,而不是无需使用@echo %var% > temp.txt的文件?我可以使用的功能也将被接受,但我特别希望使用一种无​​需使用临时文件即可对事物进行哈希处理的方法.

What I am trying to figure out is if there is maybe a specific command that I can use in case like: set /p "var=input something" && <hash command> %var% or use certutil -hashfile with %var% instead of a file without the necessary use of @echo %var% > temp.txt? A function that I could use would also be accepted but I just particularly want a method to hash things without the use of temp files.

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

So all in all, I want to be able to be able to hash something in any algorithm (md5 especially) without the use of temp files and store it in a variable.

编辑:具体来说,我想做一个新的想法:制作一个受密码保护的批处理文件,而不是仅仅通过查看就能真正轻松地找到密码.批处理文件的代码,例如,我可以输入我想要的密码的md5哈希,这样很难破解"到文件中(可以说).这样,我可以仅对用户的输入进行哈希处理,然后查看与该文件的哈希化实际密码是否相同.

EDIT: Specifically what I am trying to do is I have a new idea to make a password protected batch file where instead of being able to be able to find the password really easily by just looking into the batch file's code, I could put for example, an md5 hash of the password I want so that it would be substantially harder to "break" into the file (sort to speak). This way I could just hash the input of the user and then see if it is the same to the hashed actual password of the file.

我可以使用以下临时文件来完成我想要的东西:

I can accomplish what I am looking for with temp files with:

@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 

我有一个我想做的示例代码.我该怎么做才能做到类似于:

I have an example code on what I want to be able to do. I what to be able to do something similar to:

@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中,您可以执行以下操作:

In bash you can do this with:

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

,如果我有此文件,则可以从批处理文件中将bash用作%var%之类的文件,例如bash <filepath>\hash.sh %var,但是我想要的是一种纯批处理解决方案,而无需任何外部下载或临时文件.

and if I have this file, I could just bash it from the batch file with the arguments as %var% like bash <filepath>\hash.sh %var but what I want is a purebatch solution wihtout any external downloads or temp files.

推荐答案

就像您说的bash部分一样,您可以在bash中使用echo -n $1 | md5sum(此后的部分是多余的).但是,有一种在cmd中使用bash的方法,该方法与bash -c "<bash command>"一起使用.因此,您可以执行以下操作:

Like you said for the bash part, you can use echo -n $1 | md5sum in bash (the parts after that are redundant). However, there is a way to use bash in cmd, which is with bash -c "<bash command>". So you can do this:

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

This works since in the bash section, %var% is still a command prompt variable and gets compiled before the initial command so to the compiler it would look like bash -c "echo -n test | md5sum" where test is %var%

这篇关于有没有一种方法可以在不使用临时文件的情况下对命令输出进行哈希处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆