понедельник, 9 сентября 2019 г.

3 ways to compute file Hash

Sometimes we need to get the hash of some file.
I, for example, needed it for excluding a file from antivirus scanning.
There can be other reasons for that.

# PowersShell way
get-filehash C:\setup.exe

You may use a mini-script for saving a hash for future use.
$Hash = Get-FileHash C:\setup.exe
$Hash.Hash | Out-File C:\setup.Hash.txt

Or one-liner:
(Get-FileHash C:\setup.exe).Hash | Out-File C:\setup.Hash.txt

# Get Hash with CertUtil (CertUtil exists in every Windows)
certUtil -hashfile C:\file.zip MD5
(instead of MD5 you may use other HashAlgorithms:
MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512)

# Hash with CertUtil + PowerShell (in *nix style)
$(CertUtil -hashfile C:\TEMP\MyDataFile.img MD5)[1] -replace " ",""

# Use doskey
doskey sha1sum=powershell get-filehash -algorithm sha1 "$1"
doskey md5sum=powershell get-filehash -algorithm md5 "$1"