我正在使用以下代码块生成MD5哈希:
public static String encode(String data) throws Exception {
/* Check the validity of data */
if (data == null || data.isEmpty()) {
throw new IllegalArgumentException("Null value provided for "
+ "MD5 Encoding");
}
/* Get the instances for a given digest scheme MD5 or SHA */
MessageDigest m = MessageDigest.getInstance("MD5");
/* Generate the digest. Pass in the text as bytes, length to the
* bytes(offset) to be hashed; for full string pass 0 to text.length()
*/
m.update(data.getBytes(), 0, data.length());
/* Get the String representation of hash bytes, create a big integer
* out of bytes then convert it into hex value (16 as input to
* toString method)
*/
String digest = new BigInteger(1, m.digest()).toString(16);
return digest;
}
当我使用字符串数据[12, B006GQIIEM, MH-ANT2000]
运行上述代码段时,输出是一个31个字符的哈希值 - 268d43a823933c9dafaa4ac0e756d6a
。
MD5哈希函数存在问题还是上述代码存在问题?
0
。我在这里尝试了在线MD5生成器,也得到了相同的结果0268d43a823933c9dafaa4ac0e756d6a
。当哈希值长度不是32位时,我需要每次都在开头加上一个0
吗? - divinedragon[12,B006GQIIEM,MH-ANT2000]
吗? - Bhavik Ambani[12,B006GQIIEM,MH-ANT2000]
。实际上,在MH-ANT2000
之前有一个额外的空格。总共有两个空格。 - divinedragon