Skip to content

Instantly share code, notes, and snippets.

@tylerapplebaum
Last active January 3, 2018 18:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerapplebaum/13c8e68a233b429e3fdd3489dafe5e6f to your computer and use it in GitHub Desktop.
Save tylerapplebaum/13c8e68a233b429e3fdd3489dafe5e6f to your computer and use it in GitHub Desktop.
A function to get the secure hash of a string
Function Get-StringHash {
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$True,Mandatory=$True,HelpMessage="String to hash")]
$String,
[Parameter(HelpMessage="Hash algorithm")]
[ValidateSet('MD5','RIPEMD160','SHA1','SHA256','SHA384','SHA512')]
$Algorithm = "SHA1"
)
$StringBuilder = New-Object System.Text.StringBuilder
$ByteHash = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))
ForEach ($Byte in $ByteHash) {
[Void]$StringBuilder.Append($Byte.ToString("x2")) #Convert byte array to hex
}
$Hash = $StringBuilder.ToString()
Return $Hash
} #End Get-StringHash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment