private static byte[] hash(char[] pin, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { PBEKeySpec spec = new PBEKeySpec(pin, salt, ROUNDS, KEY_LEN); Arrays.fill(pin, Character.MIN_VALUE); try { SecretKeyFactory skf = SecretKeyFactory.getInstance(KEY_ALGORITHM); return skf.generateSecret(spec).getEncoded(); } finally { spec.clearPassword(); } }
/** * Used to combine the users password and the given salt to encrypt the users information * * @param inPassword The user defined password * @param inSalt The salt that has been randomly generated * @return Returns the users password in its hashed form * @throws Exception */ public static byte[] hash(String inPassword, byte[] inSalt) throws Exception { // Link all the elements together to form a key PBEKeySpec spec = new PBEKeySpec(inPassword.toCharArray(), inSalt, 1000, 16); try { // Generate the secret key SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); return skf.generateSecret(spec).getEncoded(); } catch (Exception e) { System.out.println(e.toString()); throw new Exception("broken hash system"); } finally { spec.clearPassword(); } }