Skip to content

Instantly share code, notes, and snippets.

@marcgeld
Last active June 22, 2017 19:11
Show Gist options
  • Save marcgeld/bf690d174d0fe9e2cfca4d6a64b12a2b to your computer and use it in GitHub Desktop.
Save marcgeld/bf690d174d0fe9e2cfca4d6a64b12a2b to your computer and use it in GitHub Desktop.
Powershell: Calculate Hashcode from byteArray
# Calculate Hashcode from byteArray
function Get-HashcodeFromByteArray {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[byte[]] $byteArray = $(Throw("-byteArray is required")),
[ValidateSet("sha256","md5")]
[string]$algorithm=("sha256")
)
Process{
Write-Verbose "Get-HashcodeFromByteArray"
$crypto = [System.Security.Cryptography.HashAlgorithm]::Create($algorithm)
$hashcode = [BitConverter]::ToString( $crypto.ComputeHash( $bytearray ) )
Write-Verbose "Hashcode $hashcode"
Write-Output $hashcode
}
}
[System.Text.Encoding] $enc = [System.Text.Encoding]::UTF8
Write-Output ( Get-HashcodeFromByteArray $enc.GetBytes( "some text to encode" ) | Out-String )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment