Java String to SHA1

You can use Apache Commons Codec (version 1.7+) to do this job for you.


Thanks to @Jon Onstott for this suggestion.

Convert your Byte Array to Hex String. Real’s How To tells you how.

return byteArrayToHexString(md.digest(convertme))

and (copied from Real’s How To)

public static String byteArrayToHexString(byte[] b) {
  String result = "";
  for (int i=0; i < b.length; i++) {
    result +=
          Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  return result;

BTW, you may get more compact representation using Base64. Apache Commons Codec API 1.4, has this nice utility to take away all the pain. refer here

Leave a Comment