Advertisement
guyrleech

Create SHA256 Hash from a String

Dec 7th, 2022
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.03 KB | Cybersecurity | 0 0
  1.  
  2. ## Modified from https://xpertkb.com/compute-hash-string-powershell/
  3.  
  4. ## Changed from original as MD5CryptoServiceProvider gives error  "This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms"
  5. ## if FIPS compliant algorithms are enforced - see https://port135.com/fips-validated-cryptographic-algorithms/
  6.  
  7. function get-hash
  8. {
  9.     Param
  10.     (
  11.         [Parameter(Mandatory=$true,HelpMessage='String to hash')]
  12.         [string]$textToHash
  13.     )
  14.  
  15.     [string]$result = $null
  16.     if( -Not [string]::IsNullOrEmpty( $textToHash ))
  17.     {
  18.         $hasher = $null
  19.         $hasher = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
  20.         if( $hasher )
  21.         {
  22.             $toHash = [System.Text.Encoding]::UTF8.GetBytes( $textToHash )
  23.             $hashByteArray = $hasher.ComputeHash( $toHash )
  24.             foreach( $byte in $hashByteArray )
  25.             {
  26.               $result = "$result{0:X2}" -f $byte
  27.             }
  28.         }
  29.     }
  30.     $result ## return
  31.  }
  32.  
Tags: sha256 hash
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement