Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
A simple yet often useful scripting task is getting the hash value for a string. Since I recently wrote this very short function for my colleague Laurent Banon (blog) allow me to share it with you.
function Hash($textToHash)
{
$hasher = new-object System.Security.Cryptography.SHA256Managed
$toHash = [System.Text.Encoding]::UTF8.GetBytes($textToHash)
$hashByteArray = $hasher.ComputeHash($toHash)
foreach($byte in $hashByteArray)
{
$res += $byte.ToString()
}
return $res;
}
And its usage :
PS #> . Hash.ps1
PS #> Hash("hi")
Instead of SHA256, you can use other hashing algorithms like the ones available by default in .NET 3.5 ou 4 :
Happy scripting!
Anonymous
April 25, 2011
The comment has been removed
Anonymous
April 27, 2011
This is not possible unless the algorithm you use is a perfect hashing function (i.e. no collision), which is a very particular case scenario. You will generally want to use hashes for passwords or for indexing.
en.wikipedia.org/.../Hash_function
Anonymous
November 18, 2013
The comment has been removed
Anonymous
September 25, 2014
Loop it seem to be like this
$res = ""
foreach($byte in $hash)
{
$res += [System.String]::Format("{0:X2}", $byte)
}
$res
Anonymous
April 22, 2015
The comment has been removed
Anonymous
September 24, 2015
would
"Simply".GetHashCode()
be ok
Please sign in to use this experience.
Sign in