Powershell MD5十六进制到字符串

Powershell MD5十六进制到字符串,powershell,Powershell,我使用一个非常简单的Powershell函数来计算MD5散列 $someString = "Hello World!" $md5 = new-object -TypeNameSystem.Security.Cryptography.MD5CryptoServiceProvider $utf8 = new-object -TypeName System.Text.UTF8Encoding $bithash = [System.BitConverter]::ToString($md5.Compute

我使用一个非常简单的Powershell函数来计算MD5散列

$someString = "Hello World!"
$md5 = new-object -TypeNameSystem.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$bithash = [System.BitConverter]::ToString($md5.ComputeHash(($utf8.GetBytes($someString))))
$hash = [convert]::tostring($bitHash,16)
Write-Host $hash
根据,System.BitConverter返回十六进制 然后我尝试将其转换为字符串的表示形式

上面的代码返回一个错误,即toString的输入格式不正确

我错过了什么


我希望在powershell中生成一个MD5函数,该函数与

的输出相匹配。如果您使用的是powershell 4,这里有一种替代方法:

$stream = New-Object System.IO.MemoryStream -ArgumentList @(,$utf8.GetBytes($someString))
$hash = Get-FileHash -Algorithm MD5 -InputStream $stream | Select-Object -ExpandProperty Hash
在PowerShell 5+中:

$stream = [System.IO.MemoryStream]::new($utf8.GetBytes($someString))
$hash = Get-FileHash -Algorithm MD5 -InputStream $stream | Select-Object -ExpandProperty Hash

我想知道Get-FileHash是否可以用于此。美好的