Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Last active July 16, 2019 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/4368545 to your computer and use it in GitHub Desktop.
Save atifaziz/4368545 to your computer and use it in GitHub Desktop.
Windows batch script to compute MD5 hash of a file using PowerShell
@echo off
if "%1"=="" echo Missing file specification>&2 & exit /b 1
PowerShell -C "[System.BitConverter]::ToString([System.Security.Cryptography.MD5]::Create().ComputeHash([System.IO.File]::ReadAllBytes('%1'))).ToLowerInvariant().Replace('-', '') + ' ' + '%~f1'"
@Kyle-2010
Copy link

Kyle-2010 commented Jul 16, 2019

You can use this code I made in Batch:
@echo off
if "%1" == "" goto blank
if "%2" == "" goto blank
if "%3" == "" goto blank
if "%4" == "" goto blank
if "%1" == "--help" goto help
if not "%1" == "--file" goto blank
if not "%3" == "--hash" goto blank
goto calc
:calc
setlocal enabledelayedexpansion
if not exist "%windir%\System32\certutil.exe" goto certerr
set filename=%2
set filehash=%4
if not exist %filename% goto ferr
if not %filehash% == MD2 (
if not %filehash% == MD4 (
if not %filehash% == MD5 (
if not %filehash% == SHA1 (
if not %filehash% == SHA256 (
if not %filehash% == SHA384 (
if not %filehash% == SHA512 (
goto herr
)
)
)
)
)
)
)
)
for /f "tokens=2" %%A in ('date /t') do set daterun=%%A
for /f "tokens=" %%B in ('time /t') do set timerun=%%B
echo HashCalc (www.hashcalc.com) 0.0.1, Time Running : %timerun%, Date Running : %daterun%
echo Calculating Hash Of This File : %filename%. Please Wait, This Will Finish Shortly...
ping localhost -n 2 -w %random% >nul 2>nul
set /a count=1
for /f "skip=1 delims=:" %%B in ('CertUtil -hashfile "%filename%" %filehash%') do (
if !count! equ 1 set "md5=%%B"
set/a count+=1
)
set "md5=%md5: =%
echo Hash Of The File %filename% : %md5%.
endlocal
exit /b
:certerr
for /f "tokens=2" %%A in ('date /t') do set daterun=%%A
for /f "tokens=
" %%B in ('time /t') do set timerun=%%B
echo HashCalc (www.hashcalc.com) 0.0.1, Time Running : %timerun%, Date Running : %daterun%
echo Error : CERT_UTIL_MISS_ERR
echo The certutil.exe File Doesn't Exist. Please Download certutil.exe, Then Move certutil.exe to Your %windir%\System32 Folder.
endlocal
exit /b
:ferr
for /f "tokens=2" %%A in ('date /t') do set daterun=%%A
for /f "tokens=" %%B in ('time /t') do set timerun=%%B
echo HashCalc (www.hashcalc.com) 0.0.1, Time Running : %timerun%, Date Running : %daterun%
echo Error : FILE_EXIST_ERR
echo The File %filename% Doesn't Exist. Type hashcalc.exe --help For Help.
endlocal
exit /b
:herr
for /f "tokens=2" %%A in ('date /t') do set daterun=%%A
for /f "tokens=
" %%B in ('time /t') do set timerun=%%B
echo HashCalc (www.hashcalc.com) 0.0.1, Time Running : %timerun%, Date Running : %daterun%
echo Error : INVALID_HASH_ERR
echo Invalid Hash. Please Input A Valid Hash. Hashes That Are Supported Are :
echo MD2, MD4, MD5, SHA1, SHA256, SHA384, Or SHA512.
exit /b
:blank
for /f "tokens=2" %%A in ('date /t') do set daterun=%%A
for /f "tokens=" %%B in ('time /t') do set timerun=%%B
echo HashCalc (www.hashcalc.com) 0.0.1, Time Running : %timerun%, Date Running : %daterun%
echo HashCalc.bat No Arguments [--help] [--file File Path\File Name] [--hash Hash Type]
echo No Arguments : Displays This Help Message, Same As Typing hashcalc.bat --help
echo --help : Displays This Help Message. Same As Typing hashcalc.bat (No Arguments)
echo --file : The Path And Name Of The File, Specified With File Path And File Name.
echo File Path : The Path To The File You Wish To Calculate The Hash Of.
echo File Name : The Name Of The File You Wish To Calculate The Hash Of.
echo --hash : The Type Of Hash Used, Specified With Hash Type.
echo Hash Type : The Type Of Hash Used To Hash The File.
endlocal
exit /b
:help
for /f "tokens=2" %%A in ('date /t') do set daterun=%%A
for /f "tokens=
" %%B in ('time /t') do set timerun=%%B
echo HashCalc (www.hashcalc.com) 0.0.1, Time Running : %timerun%, Date Running : %daterun%
echo HashCalc.bat No Arguments [--help] [--file File Path\File Name] [--hash Hash Type]
echo No Arguments : Displays This Help Message, Same As Typing hashcalc.bat --help
echo --help : Displays This Help Message. Same As Typing hashcalc.bat (No Arguments)
echo --file : The Path And Name Of The File, Specified With File Path And File Name.
echo File Path : The Path To The File You Wish To Calculate The Hash Of.
echo File Name : The Name Of The File You Wish To Calculate The Hash Of.
echo --hash : The Type Of Hash Used, Specified With Hash Type.
echo Hash Type : The Type Of Hash Used To Hash The File.
endlocal
exit /b

To run it:
Open cmd and navigate to the folder containing the file
Type in ren yourfilename.bat HashCalc.bat
Type in HashCalc.bat --help
Then follow the instructions.
Note: www.hashcalc.com is a website I made up.

@atifaziz
Copy link
Author

@Kyle-2010 Thanks for sharing your solution. I wasn't aware of certutil having that capability already so I learned something new today thanks to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment