...
Code Block | ||
---|---|---|
| ||
import java.util.Random; // ... Random number = new Random(123L); //... for (int i=0; i<20i<20; i++) { // generate another random integer in the range [0, 20] int n = number.nextInt(21); System.out.println(n); } |
...
Code Block | ||
---|---|---|
| ||
import java.security.SecureRandom; import java.security.NoSuchAlgorithmException; // ... public static void main (String args[]) { try { SecureRandom number = SecureRandom.getInstance("SHA1PRNG""SHA1PRNG"); // Generate 20 integers 0..20 for (int i = 0; i << 20; i++) { System.out.println(number.nextInt(21)); } } catch (NoSuchAlgorithmException nsae) { // Forward to handler } } |
...
Code Block | ||
---|---|---|
| ||
import java.util.Random; // ... Random number = new Random(); int n; //... for (int i=0; i<20i<20; i++) { // Re-seed generator number = new Random(); // Generate another random integer in the range [0, 20] n = number.nextInt(21); System.out.println(n); } |
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
This rule appears in the C Secure Coding Standard as MSC30-C. Do not use the rand() function for generating pseudorandom numbers.
...
Wiki Markup |
---|
\[[API 06|https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-API06]\]&nbsp;[Class Random|http://java.sun.com/javase/6/docs/api/java/util/Random.html] \[[API 06|https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-API06]\] [Class SecureRandom|http://java.sun.com/javase/6/docs/api/java/security/SecureRandom.html] \[[Find Bugs 08|https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-FindBugs08]\] BC: Random objects created and used only once \[[Monsch 06|AA. Java References#Monsch 06]\] \[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 330|http://cwe.mitre.org/data/definitions/330.html] ""Use of Insufficiently Random Values"", [CWE ID 327 |http://cwe.mitre.org/data/definitions/327.html], ""Use of a Broken or Risky Cryptographic Algorithm,"" [CWE ID 330|http://cwe.mitre.org/data/definitions/330.html], ""Use of Insufficiently Random Values"", [CWE ID 333| http://cwe.mitre.org/data/definitions/333.html] ""Failure to Handle Insufficient Entropy in TRNG"", [CWE ID 332|http://cwe.mitre.org/data/definitions/332.html] ""Insufficient Entropy in PRNG"", [CWE ID 337|http://cwe.mitre.org/data/definitions/337.html] ""Predictable Seed in PRNG"", [CWE ID 336|http://cwe.mitre.org/data/definitions/336.html] ""Same Seed in PRNG"" |
...
MSC07-J. Do not assume infinite heap space 49. Miscellaneous (MSC) MSC31-J. Never hardcode sensitive information