rspec/rules/S5344/java/how-to-fix-it/java-cryptography-extension.adoc
2025-03-24 17:50:39 +01:00

35 lines
1.1 KiB
Plaintext

== How to fix it in Java Cryptography Extension
=== Code examples
==== Noncompliant code example
The derived key is vulnerable because the cost factor (rounds) is too low for the chosen algorithm.
[source,java,diff-id=11,diff-type=noncompliant]
----
private SecretKey deriveKey(String password, byte[] salt) throws Exception {
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 120000, 256); // Noncompliant
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512");
return secretKeyFactory.generateSecret(keySpec);
}
----
==== Compliant solution
[source,javan,diff-id=11,diff-type=compliant]
----
private SecretKey deriveKey(String password, byte[] salt) throws Exception {
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 210000, 256);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512");
return secretKeyFactory.generateSecret(keySpec);
}
----
=== How does this work?
include::../../common/fix/pbkdf2-parameters.adoc[]
=== Going the extra mile
include::../../common/extra-mile/peppering.adoc[]