This page implements the main hashing methods of MD5 (128-bit), SHA-1 (160-bit), SHA256 (256-bit), SHA384 (384-bit) and SHA512 (512-bit) hashes using a string input and a hex and Base64 output.
Hashing with PowerShell |
Method
This page implements the main hashing methods of MD5 (128-bit), SHA-1 (160-bit), SHA256 (256-bit), SHA384 (384-bit) and SHA512 (512-bit) hashes using a string input and a hex and Base64 output. The code is:
$word=$Args[0] "Input word: "+$word $hash1=[System.Security.Cryptography.HashAlgorithm]::Create("md5").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($word)) "`nMD5 (Hex): "+[System.Convert]::ToHexString($hash1) "MD5 (Base64): "+[System.Convert]::ToBase64String($hash1) $hash1=[System.Security.Cryptography.HashAlgorithm]::Create("sha1").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($word)) "`nSHA-1 (Hex): "+[System.Convert]::ToHexString($hash1) "SHA-1 (Base64): "+[System.Convert]::ToBase64String($hash1) $hash1=[System.Security.Cryptography.HashAlgorithm]::Create("sha256").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($word)) "`nSHA-256 (Hex): "+[System.Convert]::ToHexString($hash1) "SHA-256 (Base64): "+[System.Convert]::ToBase64String($hash1) $hash1=[System.Security.Cryptography.HashAlgorithm]::Create("sha384").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($word)) "`nSHA-384 (Hex): "+[System.Convert]::ToHexString($hash1) "SHA-384 (Base64): "+[System.Convert]::ToBase64String($hash1) $hash1=[System.Security.Cryptography.HashAlgorithm]::Create("sha512").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($word)) "`nSHA-512 (Hex): "+[System.Convert]::ToHexString($hash1) "SHA-512 (Base64): "+[System.Convert]::ToBase64String($hash1)
We can see that we can convert from a hex string to a byte array with:
[System.Convert]::FromHexString($str1.Hash)
and then convert from a byte array (\(b\)) to a Base64 string with:
[System.Convert]::ToBase64String($b)
A sample run shows:
Input word: Hello world MD5 (Hex): 3E25960A79DBC69B674CD4EC67A72C62 MD5 (Base64): PiWWCnnbxptnTNTsZ6csYg== SHA-1 (Hex): 7B502C3A1F48C8609AE212CDFB639DEE39673F5E SHA-1 (Base64): e1AsOh9IyGCa4hLN+2Od7jlnP14= SHA-256 (Hex): 64EC88CA00B268E5BA1A35678A1B5316D212F4F366B2477232534A8AECA37F3C SHA-256 (Base64): ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw= SHA-384 (Hex): 9203B0C4439FD1E6AE5878866337B7C532ACD6D9260150C80318E8AB8C27CE330189F8DF94FB890DF1D298FF360627E1 SHA-384 (Base64): kgOwxEOf0eauWHiGYze3xTKs1tkmAVDIAxjoq4wnzjMBifjflPuJDfHSmP82Bifh SHA-512 (Hex): B7F783BAED8297F0DB917462184FF4F08E69C2D5E5F79A942600F9725F58CE1F29C18139BF80B06C0FFF2BDD34738452ECF40C488C22A7E3D80CDF6F9C1C0D47 SHA-512 (Base64): t/eDuu2Cl/DbkXRiGE/08I5pwtXl95qUJgD5cl9Yzh8pwYE5v4CwbA//K900c4RS7PQMSIwip+PYDN9vnBwNRw==