How to get an MD5 checksum in PowerShell

后端 未结 17 1565
夕颜
夕颜 2020-11-28 01:14

I would like to calculate an MD5 checksum of some content. How do I do this in PowerShell?

相关标签:
17条回答
  • 2020-11-28 01:24

    Here is a one-line-command example with both computing the proper checksum of the file, like you just downloaded, and comparing it with the published checksum of the original.

    For instance, I wrote an example for downloadings from the Apache JMeter project. In this case you have:

    1. downloaded binary file
    2. checksum of the original which is published in file.md5 as one string in the format:

    3a84491f10fb7b147101cf3926c4a855 *apache-jmeter-4.0.zip

    Then using this PowerShell command, you can verify the integrity of the downloaded file:

    PS C:\Distr> (Get-FileHash .\apache-jmeter-4.0.zip -Algorithm MD5).Hash -eq (Get-Content .\apache-jmeter-4.0.zip.md5 | Convert-String -Example "hash path=hash")
    

    Output:

    True
    

    Explanation:

    The first operand of -eq operator is a result of computing the checksum for the file:

    (Get-FileHash .\apache-jmeter-4.0.zip -Algorithm MD5).Hash
    

    The second operand is the published checksum value. We firstly get content of the file.md5 which is one string and then we extract the hash value based on the string format:

    Get-Content .\apache-jmeter-4.0.zip.md5 | Convert-String -Example "hash path=hash"
    

    Both file and file.md5 must be in the same folder for this command work.

    0 讨论(0)
  • 2020-11-28 01:27

    Here are the two lines, just change "hello" in line #2:

    PS C:\> [Reflection.Assembly]::LoadWithPartialName("System.Web")
    PS C:\> [System.Web.Security.FormsAuthentication]::HashPasswordForStoringInConfigFile("hello", "MD5")
    
    0 讨论(0)
  • 2020-11-28 01:28

    Here is the snippet that I am using to get the MD5 for a given string:

    $text = "text goes here..."
    $md5  = [Security.Cryptography.MD5CryptoServiceProvider]::new()
    $utf8 = [Text.UTF8Encoding]::UTF8
    $bytes= $md5.ComputeHash($utf8.GetBytes($text))
    $hash = [string]::Concat($bytes.foreach{$_.ToString("x2")}) 
    
    0 讨论(0)
  • 2020-11-28 01:31

    If the content is a string:

    $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)))
    

    If the content is a file:

    $someFilePath = "C:\foo.txt"
    $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    $hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)))
    

    Starting in PowerShell version 4, this is easy to do for files out of the box with the Get-FileHash cmdlet:

    Get-FileHash <filepath> -Algorithm MD5
    

    This is certainly preferable since it avoids the problems the first solution offers as identified in the comments (uses a stream, closes it, and supports large files).

    0 讨论(0)
  • 2020-11-28 01:32

    Sample for right-click menu option as well:

    [HKEY_CLASSES_ROOT\*\shell\SHA1 PS check\command]
    @="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Get-FileHash -Algorithm SHA1 '%1'"
    
    0 讨论(0)
  • 2020-11-28 01:33

    There is now a Get-FileHash function which is very handy.

    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
    

    Just change SHA384 to MD5.

    The example is from the official documentation of PowerShell 5.1. The documentation has more examples.

    0 讨论(0)
提交回复
热议问题