BSD md5 vs GNU md5sum output format?

Rio picture Rio · Aug 19, 2009 · Viewed 10.9k times · Source

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! :|

Answer

rzab picture rzab · Aug 20, 2009

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.