2024-10-29 10:36:18 +01:00
include::../description.adoc[]
2020-06-30 14:49:38 +02:00
2024-10-29 10:36:18 +01:00
As the ``++java.util.Random++`` class relies on a non-cryptographic pseudorandom number generator, this class and relating ``++java.lang.Math.random()++`` method should not be used for security-critical applications or for protecting sensitive data. In such context, the ``++java.security.SecureRandom++`` class which relies on a CSPRNG should be used in place.
2020-06-30 12:48:07 +02:00
include::../ask-yourself.adoc[]
== Recommended Secure Coding Practices
2024-10-29 10:36:18 +01:00
* Use a cryptographically secure pseudo random number generator (CSPRNG) like "java.security.SecureRandom" in place of a non-cryptographic PRNG.
2020-06-30 12:48:07 +02:00
* Use the generated random values only once.
* You should not expose the generated random value. If you have to store it, make sure that the database or file is secure.
== Sensitive Code Example
----
Random random = new Random(); // Sensitive use of Random
byte bytes[] = new byte[20];
random.nextBytes(bytes); // Check if bytes is used for hashing, encryption, etc...
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:07 +02:00
----
2024-10-29 10:36:18 +01:00
SecureRandom random = new SecureRandom();
2020-06-30 12:48:07 +02:00
byte bytes[] = new byte[20];
random.nextBytes(bytes);
----
2025-02-19 17:19:00 +01:00
include::../see.adoc[]
2021-09-21 15:40:35 +02:00
2024-08-08 09:50:21 +02:00
* OWASP - https://mas.owasp.org/checklists/MASVS-CRYPTO/[Mobile AppSec Verification Standard - Cryptography Requirements]
2024-01-15 17:15:56 +01:00
* OWASP - https://owasp.org/www-project-mobile-top-10/2016-risks/m5-insufficient-cryptography[Mobile Top 10 2016 Category M5 - Insufficient Cryptography]
2025-02-19 17:19:00 +01:00
* OWASP - https://owasp.org/www-project-mobile-top-10/2023-risks/m10-insufficient-cryptography[Mobile Top 10 2024 Category M10 - Insufficient Cryptography]
2021-09-21 15:40:35 +02:00
* https://wiki.sei.cmu.edu/confluence/x/oTdGBQ[CERT, MSC02-J.] - Generate strong random numbers
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]