How to get an MD5 checksum in PowerShell

后端 未结 17 1631
夕颜
夕颜 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:21

    This site has an example: Using Powershell for MD5 Checksums. It uses the .NET framework to instantiate an instance of the MD5 hash algorithm to calculate the hash.

    Here's the code from the article, incorporating Stephen's comment:

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

提交回复
热议问题