I am trying to generate an MD5 hex hash using the following code:
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());
The output should be 5f4dcc3b5aa765d61d8327deb882cf99
(as checked with md5sum
), but I fail to see where the error is. What am I doing wrong?