In this tutorial let’s learn about How to get MD5 checksum in PowerShell. According to RFC 1321, an MD5 checksum is a computer application that calculates and verifies 128-bit MD5 hashes. The MD5 hash (or checksum) of a file serves as a compact digital fingerprint.
Benefit of MD5 Checksum
The MD5 checksum, as we all know, is a digital fingerprint of a file. We download a lot of files from the internet without understanding the file’s integrity. As a result, an MD5 checksum, also known as an MD5 hash key, will be associated with the majority of the files. There is nothing to worry about if you download the file and the hash matches. If it fails, the file has been tampered with or modified. This could be a significant security risk or a loss of integrity.
Generate MD5 hash for 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)))
Generate MD5 checksum for file
$someFilePath = "C:\foo.txt" $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider $hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)))
MD5 checksum in PowerShell version 4 and above
The Get-FileHash cmdlet returns a file’s hash value. It uses the SHA256 algorithm by default, but we can override it with an extra argument to use MD5.
Get-FileHash [-Path] <file> [[-Algorithm] <algo>] [Options]
So, the file location is required, and we can then select the hashing algorithm and a few other choices that we don’t need in our instance.
It can also be used instead of a file path with a stream. I’ll teach you the syntax right now, and we’ll look at how to use it to hash a string in PowerShell later.
Get-FileHash <filepath> -Algorithm MD5 Get-FileHash C:\file.txt -Algorithm MD5