Weak pseudorandom number generation High

Insufficiently random generators or hardcoded seeds can make pseudorandom sequences predictable, which may lead to security vulnerabilities.

Detector ID
java/weak-random-number-generation@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1static void secureRandomSpecificAlgorithmNoncompliant() throws Exception {
2    final String ALGORITHM_NAME = "DES";
3    // Noncompliant: one specific algorithm is requested.
4    SecureRandom generator = SecureRandom.getInstance(ALGORITHM_NAME);
5    System.out.println(generator.nextInt());
6}

Compliant example

1static void secureRandomDefaultCompliant() throws Exception {
2    // Compliant: no specific algorithm is requested.
3    SecureRandom generator = new SecureRandom();
4    System.out.println(generator.nextInt());
5}