Powershell: md5sum and sha256sum Functions

Hi,

verifing of a file is often done by builting an MD5 or an sha256 hash over it.

Powershell has no “builtin” functions for this purposes.

So I wrote some 🙂

1
2
3
4
5
6
7
8
function MD5Sum([string]$sFile)
{
    $oMD5=[System.Security.Cryptography.MD5]::Create()
    $oStream=[System.IO.File]::OpenRead($sFile)
    $sMD5=[System.BitConverter]::ToString($oMD5.ComputeHash($oStream)).Replace("-" ,"").ToLower()
    $oStream.Close()
    return($sMD5)
}
1
2
3
4
5
6
7
8
function sha256sum([string]$sFile)
{
    $oSHA256=[System.Security.Cryptography.SHA256]::Create()
    $oStream=[System.IO.File]::OpenRead($sFile)
    $sSHA256=[System.BitConverter]::ToString($oSHA256.ComputeHash($oStream)).Replace("-","").ToLower()
    $oStream.Close()
    return($sSHA256)
}

Usage

1
2
PS C:\> sha256sum D:\temp\vshadow.exe
7699187b710263779b97ebe63925d77769f15236f0aa506373dd92772bbcf87b

Michael

Leave a Reply