I'm a student of computer science and we have to use BaseX (a pure Java OSS XML database) in one of our courses. While browsing through the code I discovered the following piece of code:
/**
* Returns a md5 hash.
* @param pw password string
* @return hash
*/
public static String md5(final String pw) {
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
md.update(Token.token(pw));
final TokenBuilder tb = new TokenBuilder();
for(final byte b : md.digest()) {
final int h = b >> 4 & 0x0F;
tb.add((byte) (h + (h > 9 ? 0x57 : 0x30)));
final int l = b & 0x0F;
tb.add((byte) (l + (l > 9 ? 0x57 : 0x30)));
}
return tb.toString();
} catch(final Exception ex) {
Main.notexpected(ex);
return pw;
}
}
(source: https://svn.uni-konstanz.de/dbis/basex/trunk/basex/src/main/java/org/basex/util/Token.java)
Just out of interest: what is happening there? Why these byte operations after the MD5? The docstring is saying it returns a MD5 hash...does it?