很简单的CS问题。我在读MD5文档,RFC 1321,上面写着
The algorithm takes as input a message of arbitrary length and
produces as output a 128-bit "fingerprint" or "message digest" of the input.它的意思是MD5为给定的输入生成128-bit=16字节的散列。
然后,当我在unix/macos或md5中使用MD5在线发生器脚本时,它会生成32个字符的长散列,意思是32字节。(1字符=1字节是我的理解)
例如:
$ md5 <<<"1"
b026324c6904b2a9cb4b88d6d61c81d1
$ printf "b026324c6904b2a9cb4b88d6d61c81d1" | wc -c
32但是,当我尝试使用java MD5 api时,它给出了16个字节的散列,根据文档,这是正确的。
scala> import java.security.MessageDigest
import java.security.MessageDigest
scala> MessageDigest.getInstance("MD5").digest("1".getBytes)
res0: Array[Byte] = Array(-60, -54, 66, 56, -96, -71, 35, -126, 13, -52, 80, -102, 111, 117, -124, -101)
scala> val hash = MessageDigest.getInstance("MD5").digest("1".getBytes("UTF-8")).length
hash: Int = 16问题是我在md5 (BSD工具)中遗漏了什么。
发布于 2017-04-22 08:02:07
md5的输出为十六进制,也称为基数16。在这种格式中,一个字节显示为两个字符;一个16字节哈希显示为32个字符字符串。
https://stackoverflow.com/questions/43556742
复制相似问题