Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This compliant solution uses the java.security.SecureRandom class to produce high-quality random numbers.

Code Block
bgColor#ccccff
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
// ...

public static void main (String args[]) {
   try {
     SecureRandom number = SecureRandom.getInstance("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
   }
}

...

MSC02-EX0: Using the default constructor for java.util.Random applies a seed value that is "very likely to be distinct from any other invocation of this constructor" (/[API 2006), /] and may improve security marginally. As a result, it may only be used for non-critical applications operating on non-sensitive data. Java's default seed uses the system's time in milliseconds. When used, explicit documentation of this exception is required.

Code Block
bgColor#ccccff
import java.util.Random;
// ...

Random number = new Random(); // only used for demo purposes
int n;
//...
for (int i=0; i<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);
}

For noncritical non-critical cases, such as adding some randomness to a game or unit testing, the use of class Random is acceptable. However, it is worth reiterating that the resulting low entropy random numbers are insufficiently random to be used for more serious applications, such as cryptography.

...

CERT C Secure Coding Standard

MSC30-C. Do not use the rand() function for generating pseudorandom numbers

CERT C++ Secure Coding Standard

MSC30-CPP. Do not use the rand() function for generating pseudorandom numbers

MITRE CWE

CWE ID -327, "Use of a Broken or Risky Cryptographic Algorithm"

 

CWE ID -330, "Use of Insufficiently Random Values"

 

CWE ID -332, "Insufficient Entropy in PRNG"

 

CWE ID -333, "Improper Handling of Insufficient Entropy in TRNG"

 

CWE ID -336, "Same Seed in PRNG"

 

CWE ID -337, "Predictable Seed in PRNG"

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8caf1cd12b95af70-0b417a38-416149a8-a6309d3b-1d7294cf20f9a53c7cb03cba"><ac:plain-text-body><![CDATA[

[[API 2006

https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-API06]] 

[Class Random

http://java.sun.com/javase/6/docs/api/java/util/Random.html]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bc81d28cc7f364a9-25cb67cc-48b64aed-9f788e0d-a7778047b0815679f14f02b5"><ac:plain-text-body><![CDATA[

[[API 2006

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]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bf4cd4b56785cc51-5a17c705-4e2e4565-b721a237-1bebe6977ffcca954faa3ee5"><ac:plain-text-body><![CDATA[

[[Find Bugs 2008

https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-FindBugs08]]

BC: Random objects created and used only once

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ad00232905ef4589-650e0ba5-4af04a9d-b3ed89e4-8ccfbb8b9f8b10121b599db6"><ac:plain-text-body><![CDATA[

[[Monsch 2006

AA. Bibliography#Monsch 06]]

 

]]></ac:plain-text-body></ac:structured-macro>

...