I just want to get the SHA256 checksum/hash of a string in my batch script using the Windows inbuilt certUtil utility.
I mean, I know that we can use certUtil for calculcating hashes of a file, but I just want it to calculate the hash of a string inside the batch script itself and store it as a variable like %hash%
. The string will also be a variable basically like %var%
.
Is there a simple way to do that?
Alternative/complex Approach:
I know a workaround in which we can write the variable into a file using:
echo %var% >>example.txt
And then calculating the hash of this file using:
CertUtil -hashfile "example.txt" SHA256
But this thing has its own set of problems:
- Firstly using
>>
also presses anEnter
after writing%var%
in the file, thereby changing the entire hash. - Also I can't get the hash into the variable
%hash%
in this method. I tried everything in here, but can't get anything to work. (I'm a noob at batchfile programming)
Is there a simple way to do this?