An MD5 hash and a SHA-256 hash are two fingerprints of the same bytes. They are not interchangeable. A download page that prints 32 characters is asking for MD5. One that prints 64 characters is asking for SHA-256. This guide shows how to tell them apart, how to check a file, and why neither one belongs in a password database.
Use SHA-256 when you are publishing a new checksum. Keep MD5 when an older checksum already names it.
What a hash is
A hash is a short value worked out from every byte of an input. The same input always produces the same value. Change one letter, or add a space, and the value comes out completely different. You cannot run the hash backwards and recover the original text.
People publish a hash next to a download so you can check that your copy matches theirs. The check only works when you use the algorithm they named, on the exact file they named. It does not tell you who made the file. If someone can change the file and the printed hash together, both will still match. You still have to trust the page that printed the value.
MD5 and SHA-256 are different lengths
MD5 is 128 bits, written as 32 hexadecimal characters. SHA-256 is 256 bits, written as 64. The word hello, with no space before or after it, produces these two values:
MD5: 5d41402abc4b2a76b9719d911017c592
SHA-256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
An empty input is still a hash. It is the hash of an empty string, not a missing result. MD5 of nothing is d41d8cd98f00b204e9800998ecf8427e. SHA-256 of nothing is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.
MD5 collisions can be built on purpose, so two different files can be made to share one MD5 value. That is why a new checksum should not be MD5. SHA-256 does not have a practical collision of that kind. SHA-1, which is 40 characters, has the same problem as MD5 and belongs with the older checksums. SHA-512 is 128 characters and is the longer member of the same family as SHA-256.
Which checksum to use
Read the label on the checksum you are matching.
If it says SHA-256, or the value is 64 characters, compute SHA-256. If it says MD5, or the value is 32 characters, compute MD5. A 40-character value is SHA-1. A 128-character value is SHA-512. Comparing a SHA-256 result with an MD5 result will never match, even when the file is perfect.
Letters A to F may be printed in capitals. The case does not change the value. A missing or extra character does. When you are the one publishing the checksum, publish SHA-256. Mention MD5 only when an older program still requires that name.
Check a download
Hash the file you downloaded, not the name of the file and not a sentence you typed. Use the command that matches the published algorithm.
Windows
certutil -hashfile file SHA256
Swap SHA256 for MD5 only when the publisher printed an MD5.
Mac
shasum -a 256 file
For an MD5 checksum, the command is md5 file.
Linux
sha256sum file
For an MD5 checksum, the command is md5sum file.
If the values differ, do not open the file. Download it again. Then check that you compared the right version, the right system, and the archive itself rather than a file you extracted from it. Renaming a file does not change its hash. Unzipping it does.
Hash a short phrase
A phrase is hashed as the exact characters you mean, usually as UTF-8. A space at the end is one of those characters. A line break is too.
On a Mac or Linux machine, echo hello | shasum -a 256 is not the hash of hello. The echo command adds a newline. echo -n hello | shasum -a 256 hashes the five letters alone, and that result matches the SHA-256 printed above. Windows certutil hashes a file, so put the phrase in a file first if you need the same check there.
Any program that hashes those same bytes will print the same digest. A hash generator is one way to do that for a short phrase. The command above is another. The result depends on the characters, not on which program you opened.
Do not store a password this way
MD5 and SHA-256 are fast. That is what you want for a download, and it is what you do not want for a password. A fast hash lets someone test a huge list of guesses. MD5 is also a poor fingerprint once someone can build a second input with the same value.
A password needs a slow hash built for that job, with a unique salt for each password. A checksum next to a download is a different job, and mixing the two is how password databases end up easy to crack. Short passwords make that guessing even faster.
A matching checksum also will not tell you that a program is safe to run. It tells you the bytes match a published value. Trust still sits with the publisher.
Questions about MD5 and SHA-256
No. Encryption can be reversed with the key. A hash cannot be turned back into the text. The same text always produces the same hash, and a different text produces a different one.
Copy SHA-256 unless the checksum you were given says MD5, SHA-1, or another name. A 32-character value is MD5. A 64-character value is SHA-256.
The command often hashes a trailing newline that you did not type. A space at the end of the text changes the hash too. Hash the exact characters, or hash the file itself.
No. Both are too fast for a password, and MD5 collisions can be built on purpose. Use a password hash made for that job, and use a long random password in the meantime.
Leave a Reply