Tuesday, March 20, 2012

Better checksum in Java

You were also pissed off by the need of writing shitcode like you can find on RoseIndia examples while striving to get a way to represent digest bytes as a hexadecimal string, right? Here is a better way:
MessageDigest digest = MessageDigest.getInstance("SHA1"); // Or MD5...
digest.update("hello world");
String hexdigest = new BigInteger(1, digesst.digest()).toString(16);
And that's it.

1 comment:

Unknown said...

MessageDigest.update doesn't accept string. However it's handy trick with BigNum. Thanks.