Setup: Computer with PowerShell installed

Problem: Sometimes you want to create a hash from a string.

Solution:

Run this PowerShell script or include the function in your code:

function get-hash([string]$textToHash) {
    $hasher = new-object System.Security.Cryptography.MD5CryptoServiceProvider
    $toHash = [System.Text.Encoding]::UTF8.GetBytes($textToHash)
    $hashByteArray = $hasher.ComputeHash($toHash)
    foreach($byte in $hashByteArray)
    {
      $result += "{0:X2}" -f $byte
    }
    return $result;
 }

Example:

[PS] X:\>get-hash("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua")

A704CBECAD19EE5EDFFD1D5F98A91744

Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *