我正在研究密码安全和用户登录,更具体地说,是在数据库中存储和匹配加盐密码哈希。我了解加盐和散列的基础知识,但我不明白当盐是在每个散列之前随机生成时,我应该如何在登录尝试时检查存储的散列值?
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));
}