Skip to content

Instantly share code, notes, and snippets.

@brunoleles
Last active April 8, 2017 17:11
Show Gist options
  • Save brunoleles/6639238 to your computer and use it in GitHub Desktop.
Save brunoleles/6639238 to your computer and use it in GitHub Desktop.
Calcula o HASH de uma string e retorna o valor em uma HEX String
/**
* Returns the HexString of a String
* eg.: hashDigest("hello") == "5d41402abc4b2a76b9719d911017c592"
*
* @param input input string
* @param algorithm algorithm to be used to compute the hash ( eg.: "MD5", "SHA1", "SHA-256" )
* @return the hash of the input string
*/
public static String hashDigest(String input, String algorithm)
{
try
{
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
messageDigest.update(input.getBytes());
return new BigInteger(1, messageDigest.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, e);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment