I am trying to generate sha1 of a string by referring to the example in this link - http://www.sha1-online.com/sha1-java/
public class HashTextTest {
/**
* @param args
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println(sha1("test string to sha1"));
}
static String sha1(String input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}
I dont want to have this messy line of code from string buffer - sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
.
Is there any alternate way to doing this?