Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active January 31, 2022 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/643058b57d2bf93ec28f1857cdd67e2e to your computer and use it in GitHub Desktop.
Save stknohg/643058b57d2bf93ec28f1857cdd67e2e to your computer and use it in GitHub Desktop.
文字列のハッシュ値を求める関数 (md5sum, sha1sum, sha256sum, sha384sum, sha512sumコマンドと同等の値を返す)
function Get-StringHash {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[string]$InputValue,
[ValidateSet('MD5', 'SHA1', 'SHA256', 'SHA384', 'SHA512')]
[string]$Algorithm = 'SHA256',
[switch]$ToUpperCase
)
begin {
if ([string]::IsNullOrEmpty($InputValue)) {
$InputValue = ''
}
if ([string]::IsNullOrEmpty($Algorithm)) {
$Algorithm = 'SHA256'
}
$hashAlgorithm = [Security.Cryptography.HashAlgorithm]::Create($Algorithm)
}
process {
$hashBytes = $hashAlgorithm.ComputeHash([Text.Encoding]::UTF8.GetBytes($InputValue))
if ($ToUpperCase) {
return [BitConverter]::ToString($hashBytes).Replace('-','')
}
return [BitConverter]::ToString($hashBytes).Replace('-','').ToLower()
}
end {
$hashAlgorithm.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment