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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8996820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:34:22  来源:igfitidea点击:

How to create md5 hash in bash in Mac OS X

macosbashcryptography

提问by WildBill

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

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

md5 "string"

does not return a hash.

不返回哈希值。

回答by jaypal singh

This should work -

这应该有效 -

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

or if you prefer here stringnotation then -

或者,如果您更喜欢here string符号,则 -

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

UPDATE:

更新:

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

每个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

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 newlinecharacter to produce the hash. To avoid that, you can use:

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

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

回答by user3842869

To achieve what you asked:

要实现您的要求:

md5 -s string

outputs: MD5 ("string") = b45cffe084dd3d20d928bee85e7b0f21

输出:MD5(“字符串”)= b45cffe084dd3d20d928bee85e7b0f21

回答by richo

OSX uses md5but most unices use md5sum

OSX 使用md5但大多数 unice 使用md5sum

Here is a section of rvm's rvmrc validation code which finds the correct md5 binary and wraps it.

这是rvm的 rvmrc 验证代码的一部分,它找到了正确的 md5 二进制文件并将其包装起来。

__rvm_md5_for()
{
  if builtin command -v md5 > /dev/null; then
    echo "" | md5
  elif builtin command -v md5sum > /dev/null ; then
    echo "" | md5sum | awk '{print }'
  else
    rvm_error "Neither md5 nor md5sum were found in the PATH"
    return 1
  fi

  return 0
}

( Code from https://github.com/wayneeseguin/rvm/blob/master/scripts/functions/rvmrc)

(来自https://github.com/wayneeseguin/rvm/blob/master/scripts/functions/rvmrc 的代码)

回答by Cameron E

From the command line:

从命令行:

md5 <<< "String to hash"
8a0a39505c5753ff64a0377ab0265509

回答by Kyr

All the other answers are valid. I would like to also propose opensslas well:

所有其他答案都是有效的。我还想提出以下建议openssl

? echo 'this will be hashed' | openssl md5
55be2dc2df2c1cc7bad72a0ecb338841

which is equivalent to the following

这相当于以下

? echo 'this will be hashed' | openssl dgst -md5
#?or
? openssl md5 <<< 'this will be hashed'
# or
? echo 'this will be hashed' | md5

回答by Sand1512

The correct way of doing that would be echo -n string | md5instead of echo "string" | md5. (I am using zsh)

这样做的正确方法是echo -n string | md5而不是echo "string" | md5. (我正在使用 zsh)

Convert the md5 hash given by echo -n string | md5you will get back string.

转换echo -n string | md5您给出的 md5 哈希值将返回string

md5 -s stringalso works which is already pointed out here.

md5 -s string也可以工作,这里已经指出。

λ [~] → echo "string" | md5
b80fa55b1234f1935cea559d9efbc39a

λ [~] → echo -n string | md5
b45cffe084dd3d20d928bee85e7b0f21

λ [~] → md5 -s string
MD5 ("string") = b45cffe084dd3d20d928bee85e7b0f21

回答by Aaron Spiteri

You may want to use some randomness here, otherwise the password will always be the same. This should work:

您可能希望在这里使用一些随机性,否则密码将始终相同。这应该有效:

dd if=/dev/random count=20|md5