Published

Mac OS X: Replicating md5sum Output Format

The md5sum program is used to calculate and verify 128-bit MD5 hashes. This program is installed by default in most Unix, Linux, and Unix-like operating systems including BSD. Mac OS X is a BSD variant and it also includes the md5sum program. However, the program is called md5 instead of md5sum and outputs an MD5 checksum in a different format than the standard md5sum program.

Here's what the standard md5sum output looks like:

1

2

$md5sum test.txt

d0ea20794ab78114230ba1ab167a22c2 test.txt

Now here's what the output of md5 on Mac OS X looks like:

1

2

$md5 test.txt

MD5(test.txt)=d0ea20794ab78114230ba1ab167a22c2

While this normally wouldn't be a big deal, it can cause major issues if you're trying to run scripts that were written for a Unix-like environment which expect the default md5sum format.

Thankfully, md5 has a switch that reverses the output:

1

2

$md5-rtest.txt

d0ea20794ab78114230ba1ab167a22c2 test.txt

If you'd like to permanently change md5's behavior to mimic that of md5sum, you have two options:

The first is to simply add the following alias to ~/.profile:

1

alias md5sum='md5 -r'

Now when you type 'md5sum test.txt', the command will be replaced with 'md5 -r test.txt'. However, this may not work with your scripts.

The second solution, and my preferred method, is to create a small script called md5sum that contains the following:

1

2

#!/bin/bash

/sbin/md5-r"$@"

I then make this script executable (chmod +x md5sum) and put it in /sbin/. Now, whenever a script calls md5sum, the small bash script above is used and it produces output identical to that of md5sum on other Unix systems.

I have an issue. On giving the following on the prompt:
md5sum -c 1.txt

I get the report as:

usage: md5sum [-bv] [-c [file]] | [file…]
Generates or checks MD5 Message Digests
-c check message digests (default is generate)
-v verbose, print file names when checking
-b read files in binary mode
The input for -c should be the list of message digests and file names
that is printed on stdout by this program when it generates digests.

I noticed that ‘md5sum’ on debianubuntu includes two spaces between the hash and the filename. On my Leopard mac, ‘md5 -r’ has only one space between the columns.

So look out if your script parses the output of md5 in a way that relies on that second space being present. In that case you should update your script to compensate, or otherwise somehow get the mac to include an additional space.