ps/Modules/Alkami.DevOps.Common/Public/Get-UTF8ContentHash.ps1
2023-05-30 22:51:22 -07:00

34 lines
900 B
PowerShell

function Get-UTF8ContentHash {
<#
.SYNOPSIS
Retrieves a hash generated from a UTF8 string.
.PARAMETER UTF8Content
[string] The UTF8 string to be hashed. Required.
.EXAMPLE
Get-UTF8ContentHash "Test"
.EXAMPLE
$myString = "Test"
Get-UTF8ContentHash -UTF8Content $myString
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Alias("UTF8Content")]
[string]$content
)
$encoding = New-Object System.Text.UTF8Encoding($false)
$bytes = $encoding.GetBytes($content)
$hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create("SHA256")
$hash = $hashAlgorithm.ComputeHash($bytes)
$builder = New-Object -TypeName "System.Text.StringBuilder"
foreach ($byte in $hash) {
$builder.Append($byte.ToString("x2")) | Out-Null
}
return $builder.ToString()
}