Last active
January 31, 2022 03:02
-
-
Save stknohg/643058b57d2bf93ec28f1857cdd67e2e to your computer and use it in GitHub Desktop.
文字列のハッシュ値を求める関数 (md5sum, sha1sum, sha256sum, sha384sum, sha512sumコマンドと同等の値を返す)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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