I'm researching password security and user log-in, and more specifically storing and matching salted password hashes in databases. I understand the basics of salting and hashing, but I don't understand how I am supposed to check against the stored hash-value on a log-in attempt, when the salt is randomly generated before every hash?
public static void test(String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
int iterations = 65536;
char[] passChar = password.toCharArray();
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
random.nextBytes(salt);
PBEKeySpec spec = new PBEKeySpec(passChar, salt, iterations, 512);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = factory.generateSecret(spec).getEncoded();
System.out.println(iterations + ":\n" + toHex(salt) + ":\n" + toHex(hash));
}