Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving result of CertUtil -hashfile to a variable and remove spaces of the hash

I want to save a result of

CertUtil -hashfile "path_to_file" MD5

to a variable and remove spaces of the hash in command line command (to be more particular, I wan to use this in Command Line of post-processing in VS 2015 C++).

Currently the result is as follows:

1) C:\Users\admin>CertUtil -hashfile ping.txt MD5
2) MD5 hash of file ping.txt:
3) 4f 75 c2 2c 20 b4 81 54 72 2c a5 7c 95 7a 66 88
4) CertUtil: -hashfile command completed successfully.

I just need the line 3) - save the hexa string to a variable and then remove the spaces. Thanks a lot!

like image 531
Dom Avatar asked Aug 04 '16 15:08

Dom


3 Answers

I am totally late to this, but how about just using find to get rid of the unwanted lines:

 CertUtil -hashfile "path_to_file" MD5 | find /i /v "md5" | find /i /v "certutil"
like image 65
MonkeyK Avatar answered Nov 05 '22 08:11

MonkeyK


Despite the answer above, typicall setlocal enabledelayedexpansion issue

@echo off
setlocal enabledelayedexpansion
set /a count=1 
for /f "skip=1 delims=:" %%a in ('CertUtil -hashfile "ping.txt" MD5') do (
  if !count! equ 1 set "md5=%%a"
  set/a count+=1
)
set "md5=%md5: =%
echo %md5%
endlocal
exit/B
like image 4
elzooilogico Avatar answered Nov 05 '22 09:11

elzooilogico


Use powershell command:

$(CertUtil -hashfile C:\TEMP\MyDataFile.img MD5)[1] -replace " ",""
like image 4
john Avatar answered Nov 05 '22 09:11

john