1
我找到了解决这个问题here的解决方案。从char生成MD5散列[]
private byte[] toBytes(char[] chars) {
CharBuffer charBuffer = CharBuffer.wrap(chars);
ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
byte[] bytes = Arrays.copyOfRange(byteBuffer.array(),
byteBuffer.position(), byteBuffer.limit());
Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data
Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
return bytes;
}
char[] stringChars = "String".toCharArray();
byte[] stringBytes = toBytes(stringChars);
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(stringBytes);
String stringHash = new BigInteger(1, md.digest()).toString(16);
Arrays.fill(stringChars, '\u0000');
Arrays.fill(stringBytes, (byte) 0);
但它似乎有一个错误,我不知道它在哪里或如何发生。
问题是这一部分,我想:
String hashedPass = new BigInteger(1, md.digest()).toString(16);
以上代码的输出给出字符串:
String = "9a9cce201b492954f0b06abb081d0bb4";
Correct MD5 of above string = "0e67b8eb546c322eeb39153714162ceb",
The code above though gives = "e67b8eb546c322eeb39153714162ceb";
这似乎是一个MD5的前导零丢失。
[This answer](http://stackoverflow.com/a/421696/369)建议简单左填充零 - 我不知道是否有更好的解决方案。 – Blorgbeard
@Blorgbeard非常感谢你链接那篇文章,它似乎有很多不同的(工作)解决方案的问题。比另一个更安全吗? –
如果您正在使用MD5进行需要安全的操作,则应该切换算法。你用这些哈希究竟在做什么? – Blorgbeard