title: Difference between java.util.Random and java.security.SecureRandom
body: <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 <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Random.html" rel="noreferrer">documentation</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>