我正在尝试使用以下代码生成 MD5 十六进制哈希:
String password = "password";
MessageDigest digest = MessageDigest.getInstance("MD5");
ByteArrayInputStream bais = new ByteArrayInputStream(password.getBytes());
int size = 16;
byte[] bytes = new byte[size];
while ((bais.read(bytes, 0, size)) != -1)
{
digest.update(bytes);
}
byte[] hash = digest.digest();
StringBuilder sb = new StringBuilder(2 * hash.length);
for (byte b : hash)
{
sb.append(String.format("%02x", b & 0xff));
}
System.out.println("MD5:/ " + sb.toString());
输出应该是5f4dcc3b5aa765d61d8327deb882cf99
(用 检查md5sum
),但我看不到错误在哪里。我究竟做错了什么?