Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions utility/src/main/java/com/networknt/utility/HashUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,21 @@ public static String sha256Hex (String message) {
public static String generateStrongPasswordHash(String password) throws NoSuchAlgorithmException, InvalidKeySpecException
{
int iterations = 1000;
char[] chars = password.toCharArray();
byte[] salt = getSalt().getBytes(UTF_8);

PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 64 * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = skf.generateSecret(spec).getEncoded();
return iterations + ":" + toHex(salt) + ":" + toHex(hash);
char[] chars = password.toCharArray();
PBEKeySpec spec;
try {
spec = new PBEKeySpec(chars, salt, iterations, 64 * 8);
} finally {
Arrays.fill(chars, '\0');
}
try {
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = skf.generateSecret(spec).getEncoded();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The PBKDF2 SecretKey keeps a password copy that can't be destroyed. generateSecret(spec) returns a PBKDF2KeyImpl that holds its own clone of the password in passwd, plus an HMAC keyed with the password. spec.clearPassword() doesn't touch either one, and destroy() throws DestroyFailedException. Checked on JDK 25 via reflection: passwd still holds the password after clearPassword(). It's zeroed only when the Cleaner runs after GC. The same applies at line 163 in validatePassword.

Fix: record this as a provider-owned copy in the A20 assessment, or compute PBKDF2 with a Mac over bytes you own if this copy must be ruled out.

return iterations + ":" + toHex(salt) + ":" + toHex(hash);
} finally {
spec.clearPassword();
}
}

private static String getSalt() throws NoSuchAlgorithmException
Expand Down Expand Up @@ -150,15 +158,18 @@ public static boolean validatePassword(char[] originalPassword, String storedPas
byte[] hash = fromHex(parts[2]);

PBEKeySpec spec = new PBEKeySpec(originalPassword, salt, iterations, hash.length * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] testHash = skf.generateSecret(spec).getEncoded();

int diff = hash.length ^ testHash.length;
for(int i = 0; i < hash.length && i < testHash.length; i++)
{
diff |= hash[i] ^ testHash[i];
try {
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] testHash = skf.generateSecret(spec).getEncoded();

int diff = hash.length ^ testHash.length;
for (int i = 0; i < hash.length && i < testHash.length; i++) {
diff |= hash[i] ^ testHash[i];
}
return diff == 0;
} finally {
spec.clearPassword();
}
return diff == 0;
}

/**
Expand Down
Loading