问题
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! :|
回答1:
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.
回答2:
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.
回答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.
回答4:
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.
来源:https://stackoverflow.com/questions/1299833/bsd-md5-vs-gnu-md5sum-output-format