import java.security.MessageDigest; import java.security.SecureRandom; /** * * @author : ThinkTank * */ public class PasswordDigest { /** * This method will compute hash as string for given password * * @param salt salt value * @param passDigest password digest * @param password password which will digested * @param iterationNumber number of iteration * @return password hash value * @throws Exception */ public static String recomputeHash(byte[] salt, String passDigest,String password,int iterationNumber) throws Exception { byte regeneratedHash[] = PasswordDigest.getHash(iterationNumber, password, salt); return Util.byteToBase64(regeneratedHash); } /** * @param iterationNumber * @param password * @param salt * @return hash digest as byte array * @throws Exception */ public static byte[] getHash(int iterationNumber, String password, byte[] salt) throws Exception { MessageDigest digest = MessageDigest.getInstance("SHA-512"); digest.reset(); digest.update(salt); byte[] input = digest.digest(password.getBytes("UTF-8")); for (int i = 0; i < iterationNumber; i++) { digest.reset(); input = digest.digest(input); } return input; } /** * This method will generate salt value and return it as byte array * * @return salt value as byte array * @throws Exception */ public static byte[] getSalt() throws Exception { SecureRandom secureRandom; byte[] bSalt = new byte[8]; secureRandom = SecureRandom.getInstance("SHA1PRNG","SUN"); secureRandom.nextBytes(bSalt); return bSalt; } }
Monday, July 01, 2013
Ready to use One way Digest util class in JAVA or Example of SHA-512 Digest Using Java
Labels:
java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment