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 |
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.
| Class Name | Insecure Random Number Generator (Java) | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | security | ||||||||||||||||||
| Mnemonic | JAVA.LIB.RAND.FUNC | ||||||||||||||||||
| Categories |
|
||||||||||||||||||
| Availability | Available for Java and Kotlin. |
||||||||||||||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
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.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.