6

Any one knows why BSD md5 program produces hash output in this format ...

MD5 (checksum.md5) = 9eb7a54d24dbf6a2eb9f7ce7a1853cd0

... while GNU md5sum produces much more sensible format like this?

9eb7a54d24dbf6a2eb9f7ce7a1853cd0 checksum.md5

As far as I can tell, the md5sum format is much easier to parse and makes more sense. How do you do md5sum -check with md5? And what do the -p, -q, -r, -t, -x options mean? man md5 says nothing about those options! :|

  • could go on either poweruser/serverfault but doesn't really belong here. – spender Aug 19 '09 at 13:25
  • 3
    ... or is it superuser? I never remember – spender Aug 19 '09 at 13:29
  • `md5 -r` will produce something remarkably similar to `md5sum` (checksum, then filename). For example, `find . -type f -print0 | xargs -0 md5 -r` lists the md5 checksums for all your files. – Dave Jul 17 '15 at 13:27
  • 1
    The utility of the preceding `MD5` could be that the hash algorithm can be deduced from the file itself, rather than assumed. Consider that `9eb7a54d24dbf6a2eb9f7ce7a1853cd0 myfile.txt` doesn't tell you what the hash used was. Also consider the complete mess created by having "plain text" without including the file encoding anywhere in it. By including the hash algorithm, it's unambiguous what hash should be used for comparison. BSD's format seems *more* sensible to me. – jpmc26 Jul 07 '17 at 20:23

4 Answers4

6

On current OS X BSD systems you can specify the md5 -r command to get the expected output.

sgwilbur@gura:/vms/DevOps-v3.4$ md5 vmware*
MD5 (vmware-0.log) = 61ba1d68a144023111539abee08f4044
MD5 (vmware-1.log) = 97bc6f22b25833c3eca2b2cc40b83ecf
MD5 (vmware-2.log) = f92a281102710c4528d4ceb88aa0ac9b
MD5 (vmware.log) = 1f7858d361929d4bc5739931a075c0ad

Adding the md5 -r switch made the output look more like I was expecting, and easier to diff with the linux md5 sums that were produced from a Linux machine.

sgwilbur@gura:/vms/DevOps-v3.4$ md5 -r vmware*
61ba1d68a144023111539abee08f4044 vmware-0.log
97bc6f22b25833c3eca2b2cc40b83ecf vmware-1.log
f92a281102710c4528d4ceb88aa0ac9b vmware-2.log
1f7858d361929d4bc5739931a075c0ad vmware.log

This was the simplest approach for me to make is easy to diff from output generated by the md5sum command on a linux box.

Sean
  • 643
  • 8
  • 10
5

Historical reasons, i guess. Meanwhile, -q suppress "MD5(...) = " output, so md5 -q checksum.md5 gives

9eb7a54d24dbf6a2eb9f7ce7a1853cd0

This is implied if md5 is not given any arguments and it reads from stdin. Unfortunately md5sum in this case leaves "-" behind the checksum ("9eb7a54d24dbf6a2eb9f7ce7a1853cd0 -"), so if you're looking for some generic function to return the checksum, here is what might help:

checksum() {
        (md5sum <"$1"; test $? = 127 && md5 <"$1") | cut -d' ' -f1
}
checksum /etc/hosts

FreeBSD's man page says about the arguments

   -p      Echo stdin to stdout and append the checksum to stdout.

 -q      Quiet mode ‐ only the checksum is printed out.  Overrides the -r
         option.

 -r      Reverses the format of the output.  This helps with visual diffs.
         Does nothing when combined with the -ptx options.

 -t      Run a built‐in time trial.

 -x      Run a built‐in test script.

rzab
  • 1,657
  • 11
  • 7
3

I realize this is an old page, but I was making checksums on FreeBSD and checking them on Linux and I came across this page too. This page didn't help me solve the problem, so I came up with this small sed script to create the checksums on FreeBSD that match the Linux md5sum output:

md5 file [file ...] | sed -e 's#^MD5 [(]\(.*\)[)] = \(.*\)$#\2 \1#' > md5sums.txt

This will use the FreeBSD md5 command and rearrange the output to look like the GNU md5sum.

Then on Linux I can just use md5sum --check md5sums.txt

You can also use the above sed script with an existing file produced by FreeBSD's md5 command.

I also put this alias in my FreeBSD .cshrc file:

alias md5sum "md5 \!* | sed -e '"'s#MD5 [(]\(.*\)[)] = \(.*\)$#\2 \1#'"'"

now on FreeBSD I can just say md5sum file1 file2 file3 ... and it just works.

Brian Onn
  • 1,026
  • 11
  • 18
  • the sed script gave me one less space than the GNU md5sum on centos6 did. NBD, but "diff" didn't work without massaging it slightly. – Dan Pritts Dec 03 '14 at 20:02
  • I am not very good with sed, so this script was helpful. Added the extra space as Dan metions. Thanks. – Ted Aug 21 '22 at 23:46
0

One can use the GNU md5sum -c checksum.md5 that will look for checksum file and check against the checksum.md5 file content.

md5sum -c checksum.md5 | grep "checksum: OK" -

Example inside a Ruby system call to check against a BSD formatted .md5 file:

system("md5sum -c checksum.md5 | grep \"checksum: OK\" -")

This will return true or false.

pedro mota
  • 11
  • 3