rspec/rules/S2053/java/rule.adoc

51 lines
1.1 KiB
Plaintext

== Why is this an issue?
include::../description.adoc[]
include::../recommended.adoc[]
=== Noncompliant code example
Below, the hashed password use a predictable salt:
[source,java]
----
byte[] salt = "notrandom".getBytes();
PBEParameterSpec cipherSpec = new PBEParameterSpec(salt, 10000); // Noncompliant, predictable salt
PBEKeySpec spec = new PBEKeySpec(chars, salt, 10000, 256); // Noncompliant, predictable salt
----
=== Compliant solution
Use ``++java.security.SecureRandom++`` to generate an unpredictable salt:
[source,java]
----
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
PBEParameterSpec cipherSpec = new PBEParameterSpec(salt, 10000); // Compliant
PBEKeySpec spec = new PBEKeySpec(chars, salt, 10000, 256); // Compliant
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]