Generate & Compare MD5, SHA1 Hashes For Files In Terminal 0


Generate & Compare MD5, SHA1 Hashes For Files In Terminal


A while back I mentioned an app that can compare files, this worked by comparing the hash of a file, essentially its make up at the byte level. This means, even though two files that are the same size in terms of bits, will be different because of the order of those bits generate a different hash number. This is very useful in comparing different files and ensuring you are working with the right file. All you have to do is compare the hashes of the two files and if they match you are good to go. Today’s post will show you how you generate MD5 and SHA-1 hashes in Terminal.

Generating MD5

The first stage is to open up Terminal and type the following to generate an MD5 hash.


Then either type the file name and/or path, or better yet drop the file from Finder into the Terminal window, ensure there is a space after the md5. This will generate an ouput similar to the following:

md5 file.png
MD5 (file.png) = 251b5c95860dca0b07b15e927fc0cc84

Very simple.

Generating SHA-1

Now, there is a better hash function called SHA-1, that produces a more accurate hash of the file you are wanting to check. To generate an SHA-1 file you need to use openssl, which should be installed on your Mac by default. To generate an SHA-1 hash type in the following, followed by the path to the file, or the file dropped onto the Finder window:

openssl sha1

For example:

openssl sha1 file.png
SHA1(file.png)= ba45256be13be6d30361e4541c5dfbe381c1c3be

Notice the longer number, you can use openssl for md5, just enter md5 instead of sha1.

This tip is very useful if you want to compare the hashes of the files you have downloaded from the Apple downloads page, for example this page has a hash at the bottom which you can compare to the one you have created for the file on your computer.

Multiple Files

If you want to generate hashes for multiple files, its really simple. The best method is to use Finder and openssl. In Finder type the command you want for the hash function you want to run (eg openssl sha1) and then drop all of the files you want to generate hashes for on to the Terminal window from Finder. Terminal will run through each file and output the hash function.

Terminal generating an sha1 hash for a group of files.

The hash function will take some time if you have massive files, but will quickly run on smaller files. I use this when I upload files to my server, or move them around on various networks, to ensure both files are exactly the same.

If you have any questions for comments, please leave one below.

If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips