Calculating a string's hash code via .NET cryptography

suggest change

Use the System.Security.Cryptography.HashAlgorithm class to hash a string with a supported algorithm.

$example="Nobody expects the Spanish Inquisition."

#calculate
$hash=[System.Security.Cryptography.HashAlgorithm]::Create("sha256").ComputeHash(
[System.Text.Encoding]::UTF8.GetBytes($example))

#convert to hex
[System.BitConverter]::ToString($hash) 

#2E-DF-DA-DA-56-52-5B-12-90-FF-16-FB-17-44-CF-B4-82-DD-29-14-FF-BC-B6-49-79-0C-0E-58-9E-46-2D-3D

The "sha256" part was the hash algorithm used.

The hyphens can be removed, and the hex digits can be changed to lowercase:

#convert to lower case hex without '-' 
[System.BitConverter]::ToString($hash).Replace("-","").ToLower()

#2edfdada56525b1290ff16fb1744cfb482dd2914ffbcb649790c0e589e462d3d

If you prefer Base64, convert the hash bytes:

#convert to base64
[Convert]::ToBase64String($hash)

#Lt/a2lZSWxKQ/xb7F0TPtILdKRT/vLZJeQwOWJ5GLT0=

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents