JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.LIB.RAND.FUNC : Insecure Random Number Generator (Java)

要旨

An insecure random number generator is used instead of a secure one.

Random numbers can be generated with both the java.util.Random and the java.security.SecureRandom class. However, the latter generates cryptographically secure random numbers and is hence preferred. Moreover, recreating the random generator every time that a random number is needed results in a waste of resources. Java 17 provides the java.util.random.RandomGenerator interface. The objects implementing RandomGenerator are typically not cryptographically secure. Note, however, that SecureRandom does implement the RandomGenerator interface, so that instances of SecureRandom may be used interchangeably with other types of pseudorandom generators in applications that do not require a secure generator.

プロパティ

クラス名 Insecure Random Number Generator (Java)
日本語クラス名 Insecure Random Number Generator (Java)
クラス分類 セキュリティ (security)
ニーモニック JAVA.LIB.RAND.FUNC
カテゴリー
CWE CWE:330 Use of Insufficiently Random Values
CERT-Java CERT-Java:MSC02-J Generate strong random numbers
OWASP-2021 OWASP-2021:A2 Cryptographic failures
  OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A04 Cryptographic Failures
  OWASP-2025:A06 Insecure Design
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Insecure Random Number Generator (Java)"

import java.util.Random;
import java.security.SecureRandom;

public class Main {
  public static void main(String[] args) {
      Random r = new Random();                                    // Insecure Random Number Generator (Java) warning issued here
      int[] array = mkRandomArray(Math.abs(r.nextInt() % 1000));  // Single-Use Random Number Generator (Java) warning issued here
      for (int i: array)
        System.out.println(i);
      Random sr = new SecureRandom(new byte[]{1,1,1,1});          // Hardcoded Random Seed (Java) warning issued here
  }

  private static int[] mkRandomArray(int length) {
    int[] result = new int[length];
    for (int pos = 0; pos < length; pos++)
      result[pos] = new Random().nextInt();                       /* Warnings of two classes issued here:
                                                                   * - Insecure Random Number Generator (Java)
                                                                   * - Single-Use Random Number Generator (Java)
                                                                   */
    return result;
  }
}

To resolve these issues, the program could be modified as follows.

import java.security.SecureRandom;
import java.util.Random;

public class Main {
  private final static Random r = new SecureRandom();

  public static void main(String[] args) {
      int[] array = mkRandomArray(Math.abs(r.nextInt() % 1000));
      for (int i: array)
          System.out.println(i);
      Random sr = new SecureRandom();
  }

  private static int[] mkRandomArray(int length) {
      int[] result = new int[length];
      for (int pos = 0; pos < length; pos++)
          result[pos] = r.nextInt();
      return result;
  }
}

解決法

Use java.security.SecureRandom instead of java.util.Random. Store the random generator in a field instead of a local variable.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.