在Linux和Python中的MD5是什么?

3

我在Python和Linux中使用MD5算法对相同的字符串进行哈希,但是得到了不同的值,有人可以指出问题在哪里吗?

在Linux中: echo "logdir" | md5sum - | awk '{print $1}'

gives: aba76197efa97e6bd4e542846471b391 

在Python中:
md5.new("logdir".encode('utf-8')).hexdigest()
意思是对字符串"logdir"进行UTF-8编码并计算其MD5哈希值。
gives: ee6da4c228cfaebfda7f14e4371a097d
1个回答

6

echo 命令会自动换行,除非你使用 echo -n 明确告诉它不要换行。

$ echo -n "logdir" | md5sum - | awk '{print $1}'
ee6da4c228cfaebfda7f14e4371a097d

来自echo命令的说明文档:

DESCRIPTION
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接