BSD md5 与 GNU md5sum 输出格式?

发布于 2024-08-01 20:34:44 字数 369 浏览 14 评论 0原文

任何人都知道为什么 BSD md5 程序会产生这种格式的哈希输出……

MD5 (checksum.md5) = 9eb7a54d24dbf6a2eb9f7ce7a1853cd0

而 GNU md5sum 会产生这样更合理的格式?

9eb7a54d24dbf6a2eb9f7ce7a1853cd0 checksum.md5

据我所知,md5sum 格式更容易解析并且更有意义。 如何使用 md5 进行md5sum -check? -p、-q、-r、-t、-x 选项是什么意思? man md5 没有提及这些选项! :|

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦初醒的夏天 2024-08-08 20:34:45

在当前的 OS X BSD 系统上,您可以指定 md5 -r 命令来获取预期的输出。

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

添加 md5 -r 开关使输出看起来更像我所期望的,并且更容易与 Linux 机器生成的 linux md5 和进行比较。

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

对我来说,这是最简单的方法,很容易与 Linux 机器上的 md5sum 命令生成的输出进行区分。

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.

夏至、离别 2024-08-08 20:34:45

我猜是历史原因。 同时, -q 抑制 "MD5(...) = " 输出,因此 md5 -q checksum.md5 给出

9eb7a54d24dbf6a2eb9f7ce7a1853cd0

如果 md5 没有给出任何参数并且它从 stdin 读取,则暗示这一点。
不幸的是,在这种情况下,md5sum 在校验和后面留下了“-”(“9eb7a54d24dbf6a2eb9f7ce7a1853cd0 -”),
因此,如果您正在寻找一些通用函数来返回校验和,以下内容可能会有所帮助:

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

FreeBSD 的手册页介绍了这些参数

   -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.

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.

独行侠 2024-08-08 20:34:45

我意识到这是一个旧页面,但我在 FreeBSD 上进行校验并在 Linux 上检查它们,我也遇到了这个页面。 这个页面没有帮助我解决问题,所以我想出了这个小 sed 脚本来在 FreeBSD 上创建与 Linux md5sum 输出匹配的校验和:

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

这将使用FreeBSD md5 命令并重新排列输出,使其看起来像 GNU md5sum

然后在 Linux 上我可以使用 md5sum --check md5sums.txt

您还可以将上面的 sed 脚本与 FreeBSD 的 md5 命令。

我还将这个别名放入我的 FreeBSD .cshrc 文件中:

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

现在在 FreeBSD 上我只需说 md5sum file1 file2 file3 ... 就可以了。

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.

遗弃M 2024-08-08 20:34:45

可以使用 GNU md5sum -c checksum.md5 来查找 checksum 文件并检查 checksum.md5 文件内容。

md5sum -c 校验和.md5 | grep "checksum: OK" -

Ruby 系统调用中的示例,用于检查 BSD 格式的 .md5 文件:

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

这将返回 true 或 false。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文