Luk*_*101 157 powershell powershell-2.0
我想计算一些内容的MD5校验和.我如何在PowerShell中执行此操作?
vcs*_*nes 297
如果内容是字符串:
$someString = "Hello, World!"
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = New-Object -TypeName System.Text.UTF8Encoding
$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($someString)))
Run Code Online (Sandbox Code Playgroud)
如果内容是文件:
$someFilePath = "C:\foo.txt"
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)))
Run Code Online (Sandbox Code Playgroud)
从PowerShell版本4开始,对于Get-FileHashcmdlet 开箱即用的文件很容易做到:
Get-FileHash <filepath> -Algorithm MD5
Run Code Online (Sandbox Code Playgroud)
这当然是首选,因为它避免了第一个解决方案提供的问题,如注释中所标识的(使用流,关闭它,并支持大文件).
Kei*_*ill 57
如果您使用的是PowerShell社区扩展,则可以使用Get-Hash命令行开关轻松完成此操作:
C:\PS> "hello world" | Get-Hash -Algorithm MD5
Algorithm: MD5
Path :
HashString : E42B054623B3799CB71F0883900F2764
Run Code Online (Sandbox Code Playgroud)
Avk*_*han 15
以下是两行,只需更改第2行中的"hello":
PS C:\> [Reflection.Assembly]::LoadWithPartialName("System.Web")
PS C:\> [System.Web.Security.FormsAuthentication]::HashPasswordForStoringInConfigFile("hello", "MD5")
Run Code Online (Sandbox Code Playgroud)
Dav*_*vid 14
这是我用来处理相对和绝对路径的函数:
function md5hash($path)
{
$fullPath = Resolve-Path $path
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$file = [System.IO.File]::Open($fullPath,[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
try {
[System.BitConverter]::ToString($md5.ComputeHash($file))
} finally {
$file.Dispose()
}
}
Run Code Online (Sandbox Code Playgroud)
感谢上面的@davor建议使用Open()而不是ReadAllBytes()和@jpmc26来建议使用finally块.
在线使用ComputeHash()有很多例子.我的测试表明,当通过网络连接运行时,这非常慢.下面的代码段对我来说运行得更快,但是YMMV:
$md5 = [System.Security.Cryptography.MD5]::Create("MD5")
$fd = [System.IO.File]::OpenRead($file)
$buf = New-Object byte[] (1024*1024*8) # 8 MB buffer
while (($read_len = $fd.Read($buf,0,$buf.length)) -eq $buf.length){
$total += $buf.length
$md5.TransformBlock($buf,$offset,$buf.length,$buf,$offset)
Write-Progress -Activity "Hashing File" `
-Status $file -percentComplete ($total/$fd.length * 100)
}
# Finalize the last read
$md5.TransformFinalBlock($buf, 0, $read_len)
$hash = $md5.Hash
# Convert hash bytes to a hexadecimal formatted string
$hash | foreach { $hash_txt += $_.ToString("x2") }
Write-Host $hash_txt
Run Code Online (Sandbox Code Playgroud)
小智 7
现在有一个非常方便的 Get-FileHash 函数。
PS C:\> Get-FileHash C:\Users\Andris\Downloads\Contoso8_1_ENT.iso -Algorithm SHA384 | Format-List
Algorithm : SHA384
Hash : 20AB1C2EE19FC96A7C66E33917D191A24E3CE9DAC99DB7C786ACCE31E559144FEAFC695C58E508E2EBBC9D3C96F21FA3
Path : C:\Users\Andris\Downloads\Contoso8_1_ENT.iso
Run Code Online (Sandbox Code Playgroud)
只需更改SHA384为MD5.
该示例来自 PowerShell 5.1 的官方文档。该文档有更多示例。
正如接受的答案中所述,Get-FileHash很容易与文件一起使用,但也可以将它与字符串一起使用:
$s = "asdf"
Get-FileHash -InputStream ([System.IO.MemoryStream]::New([System.Text.Encoding]::ASCII.GetBytes($s)))
Run Code Online (Sandbox Code Playgroud)
小智 7
PowerShell One-Liners(字符串到哈希)
MD5
([System.BitConverter]::ToString((New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider).ComputeHash((New-Object -TypeName System.Text.UTF8Encoding).GetBytes("Hello, World!")))).Replace("-","")
Run Code Online (Sandbox Code Playgroud)
SHA1
([System.BitConverter]::ToString((New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider).ComputeHash((New-Object -TypeName System.Text.UTF8Encoding).GetBytes("Hello, World!")))).Replace("-","")
Run Code Online (Sandbox Code Playgroud)
SHA256
([System.BitConverter]::ToString((New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider).ComputeHash((New-Object -TypeName System.Text.UTF8Encoding).GetBytes("Hello, World!")))).Replace("-","")
Run Code Online (Sandbox Code Playgroud)
SHA384
([System.BitConverter]::ToString((New-Object -TypeName System.Security.Cryptography.SHA384CryptoServiceProvider).ComputeHash((New-Object -TypeName System.Text.UTF8Encoding).GetBytes("Hello, World!")))).Replace("-","")
Run Code Online (Sandbox Code Playgroud)
SHA512
([System.BitConverter]::ToString((New-Object -TypeName System.Security.Cryptography.SHA512CryptoServiceProvider).ComputeHash((New-Object -TypeName System.Text.UTF8Encoding).GetBytes("Hello, World!")))).Replace("-","")
Run Code Online (Sandbox Code Playgroud)
该网站有一个例子:http://blog.brianhartsock.com/2008/12/13/using-powershell-for-md5-checksums/.它使用.NET框架实例化MD5哈希算法的实例来计算哈希值.
以下是文章中的代码,其中包含Stephen的评论:
param
(
$file
)
$algo = [System.Security.Cryptography.HashAlgorithm]::Create("MD5")
$stream = New-Object System.IO.FileStream($Path, [System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read)
$md5StringBuilder = New-Object System.Text.StringBuilder
$algo.ComputeHash($stream) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
$md5StringBuilder.ToString()
$stream.Dispose()
Run Code Online (Sandbox Code Playgroud)
小智 5
默认情况下,可以追溯到2003年的另一个内置命令是Windows,它当然也可以从powershell中调用.
__CODE__
(警告:MD5应全部采用上限以获得最大的稳健性)
| 归档时间: |
|
| 查看次数: |
229260 次 |
| 最近记录: |