Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between java.util.Random and java.security.SecureRandom
    primarykey
    data
    text
    <p>My team got handed over some server side code (in Java) that generates random tokens and I have a question regarding the same -</p> <p>The purpose of these tokens is fairly sensitive - used for session id, password reset links etc. So they do need to be cryptographically random to avoid somebody guessing them or brute force them feasibly. The token is a "long" so it is 64 bits long.</p> <p>The code currently uses the <code>java.util.Random</code> class to generate these tokens. The documentation ([<a href="http://docs.oracle.com/javase/7/docs/api/java/util/Random.html][1]" rel="noreferrer">http://docs.oracle.com/javase/7/docs/api/java/util/Random.html][1]</a>) for <code>java.util.Random</code> clearly states the following: </p> <blockquote> <p>Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.</p> </blockquote> <p>However, the way the code is currently using <code>java.util.Random</code> is this - It instantiates the <code>java.security.SecureRandom</code> class and then uses the <code>SecureRandom.nextLong()</code> method to obtain the seed that is used for instantiating the <code>java.util.Random</code>class. Then it uses <code>java.util.Random.nextLong()</code> method to generate the token.</p> <p>So my question now - Is it still insecure given that the <code>java.util.Random</code> is being seeded using <code>java.security.SecureRandom</code>? Do I need to modify the code so that it uses <code>java.security.SecureRandom</code> exclusively to generate the tokens?</p> <p>Currently the code seed's the <code>Random</code> once at startup</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload