'Is there a way to hash a command output without the use of a temp file?

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?

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.


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.

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)

In bash you can do this with:

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

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.



Solution 1:[1]

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

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%

Solution 2:[2]

You can also do this in 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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nico Nekoru
Solution 2 Nico Nekoru