Last active
April 8, 2017 17:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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