MD5 and SHA1 hashes in Powershell (4 functions)


To totally unlock this section you need to Log-in


Login
The following functions can be used, in Powershell, to calculate easily the SHA1 and MD5 hashes of a file, or text strings.

MD5

MD5 is an algorithm that is used to verify data integrity through the creation of a 128-bit message digest from data input (which may be a message of any length) that is claimed to be as unique to that specific data as a fingerprint is to the specific individual.

History of MD5

MD5, which was developed by Professor Ronald L. Rivest of MIT, is intended for use with digital signature applications, which require that large files must be compressed by a secure method before being encrypted with a secret key, under a public key cryptosystem. MD5 is currently a standard, Internet Engineering Task Force (IETF) Request for Comments (RFC) 1321.

This first function will compute a file (specified as a parameter when function is called) using the MD5 algorithm:

function md5hash($path)

{
$fullPath = Resolve-Path $path
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$file = [System.IO.File]::Open($fullPath,[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
[System.BitConverter]::ToString($md5.ComputeHash($file))
$file.Dispose()
}

To call this function, in a Powershell script, you'll have to write down this, for example:

md5hash("C:\myprogram\example.exe")

SHA1

SHA-1 (Secure Hash Algorithm) is a most commonly used from SHA series of cryptographic hash functions, designed by the National Security Agency of USA and published as their government standard.

SHA-1 produce the 160-bit hash value. Original SHA (or SHA-0) also produce 160-bit hash value, but SHA-0 has been withdrawn by the NSA shortly after publication and was superseded by the revised version commonly referred to as SHA-1. The other functions of SHA series produce 224-, 256-, 384- and 512-bit hash values.

History of SHA series

SHA-0 published in 1993 as the Secure Hash Standard, FIPS PUB 180 by National Institute of Standards and Technology.

SHA-1 published in 1995 in FIPS PUB 180-1.

SHA-256, SHA-384 and SHA-512 first published in 2001 as draft FIPS PUB 180-2 and released as official standard in 2002.

SHA-224 published in 2004 as change notice for FIPS PUB 180-2.

This second function will compute a file (specified as a parameter when function is called) using the SHA1 algorithm:

function sha1hash($path)

{
$fullPath = Resolve-Path $path
$sha1 = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
$file = [System.IO.File]::Open($fullPath,[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
[System.BitConverter]::ToString($sha1.ComputeHash($file))
$file.Dispose()
}

As the MD5 algorithm above, to call this function you'll have to specify the full path of the file you'll have to compute the message digest (hash):

sha1hash("C:\myprogram\example.exe")

Hashing Strings

To create hash values from text blocks you could use the following functions to create MD5 and SHA1 values for text strings.

[tweet]

This first function will compute the MD5 hash of a text string, passed as always as a parameter:

function md5strhash($string)

{
$inputString = $string
$cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider];
$hashAlgorithm = new-object $cryptoServiceProvider
$hashByteArray = $hashAlgorithm.ComputeHash([Char[]]$inputString);
foreach ($byte in $hashByteArray) { $result += “{0:X2}” -f $byte }
Write-Host $result
}

Similarly we could compute the SHA1 of a string by using the following function:

function sha1strhash($string)

{
$inputString = $string
$cryptoServiceProvider = [System.Security.Cryptography.SHA1CryptoServiceProvider];
$hashAlgorithm = new-object $cryptoServiceProvider
$hashByteArray = $hashAlgorithm.ComputeHash([Char[]]$inputString);
foreach ($byte in $hashByteArray) { $result += “{0:X2}” -f $byte }
Write-Host $result
}

There are a lot of other CryptoServiceProviders available in Powershell: http://msdn.microsoft.com/en-us/library/system.security.cryptography(v=vs.110).aspx

The other available hash algorithms are:

  • SHA256
  • SHA384
  • SHA512
  • Enjoy. :)