如何在 Mac OS X 的 bash 中创建 md5 哈希 [英] How to create md5 hash in bash in Mac OS X

查看:25
本文介绍了如何在 Mac OS X 的 bash 中创建 md5 哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Mac 上使用 bash 为字符串创建 md5 哈希?md5sum 在我的环境中不存在.我为 md5 做了一个 man 但我对它的真正作用感到困惑.

How can you create an md5 hash for a string on a mac using bash? md5sum does not exist in my environment. I did a man for md5 but I'm confused about what that really does.

md5 "string"

不返回哈希值.

推荐答案

这应该可行 -

[jaypal:~/Temp] echo "this will be encrypted" | md5
72caf9daf910b5ef86796f74c20b7e0b

或者如果您更喜欢 here string 表示法,那么 -

or if you prefer here string notation then -

[jaypal:~/Temp] md5 <<< 'this will be encrypted'
72caf9daf910b5ef86796f74c20b7e0b

更新:

根据 man 页面,您可以使用以下任何选项

UPDATE:

Per the man page, you can play around with any of the following options

-s string
        Print a checksum of the given string.

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

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


[jaypal:~/Temp] md5 -s 'this will be encrypted'
MD5 ("this will be encrypted") = 502810f799de274ff7840a1549cd028a

[jaypal:~/Temp] md5 -qs 'this will be encrypted'
502810f799de274ff7840a1549cd028a

注意:MD5 总是产生相同的哈希值.您发现输出与上面给出的示例不同的原因是由于评论中提到的一点.前两个示例使用尾随的 newline 字符来生成散列.为避免这种情况,您可以使用:

Note: MD5 always produces the same hash. The reason you find the output different from the example given above is due to a point that has been made in the comments. The first two examples use the trailing newline character to produce the hash. To avoid that, you can use:

[jaypal:~/Temp] echo -n "this will be encrypted" | md5
502810f799de274ff7840a1549cd028a

例如,如果你使用 echo -n "string";|md5(注意 -n 选项),你会得到 b45cffe084dd3d20d928bee85e7b0f21.但是,如果你使用 echo "string";|md5,你会得到 b80fa55b1234f1935cea559d9efbc39a.

For example, if you use echo -n "string" | md5 (note the -n option), you get b45cffe084dd3d20d928bee85e7b0f21. But, if you use echo "string" | md5, you get b80fa55b1234f1935cea559d9efbc39a.

或者,使用 shell 进行验证:

Or, verify it with the shell:

➜  [jaypal:~/Temp] [ $(echo "HOLA" | md5) = $(echo "HOLA" -n | md5) ]; echo "$?"
1
# 1 -> False. Hence, the result from echoing "HOLA" toggling the -n flag
# outputs different md5 checksums.

这篇关于如何在 Mac OS X 的 bash 中创建 md5 哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆