macos - 如何在 Mac OS X 中的 bash 中创建 md5 哈希

标签 macos bash cryptography

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

md5 "string"

不返回哈希。

最佳答案

这应该有效-

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

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

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

更新:

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

-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 字符来生成散列。为避免这种情况,您可以使用:

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

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

或者,用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.

关于macos - 如何在 Mac OS X 中的 bash 中创建 md5 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8996820/

相关文章:

hash - Merkle-Damgård 结构中的河豚?

ios - Silverlight 开发兼容性问题

bash - 带空格的 Shell 变量,引用单个命令行选项

php - 为什么在解密使用 PHP 加密的值时,decipherIv.final() 在 Node.js 中失败?

regex - 在bash中使用sed换行,正则表达式问题

Python 脚本未在 cron 中运行

c# - 如何检查 Asp.NET 应用程序是否支持 FIPS

Swift 将命令传递到 jar 文件中

mysql - El Capitan DP8 是否在 MAMP PRO 3.4 中断开了与 LocalHost 的连接?

macos - native 库 lz4 不适用于 Spark