Search code examples
javarandomsecurityuuid

Is UUID.randomUUID() suitable for use as a one-time password?


As previous discussed, confirmation emails should have a unique, (practically) un-guessable code--essentially a one-time password--in the confirmation link.

The UUID.randomUUID() docs say:

The UUID is generated using a cryptographically strong pseudo random number generator.

Does this imply that the the UUID random generator in a properly implemented JVM is suitable for use as the unique, (practically) un-guessable OTP?


Solution

  • No. According to the UUID spec:

    Do not assume that UUIDs are hard to guess; they should not be used as security capabilities (identifiers whose mere possession grants access), for example. A predictable random number source will exacerbate the situation.

    Also, UUIDs only have 16 possible characters (0 through F). You can generate a much more compact and explicitly secure random password using SecureRandom (thanks to @erickson).

    import java.security.SecureRandom;
    import java.math.BigInteger;
    
    public final class PasswordGenerator {
        private SecureRandom random = new SecureRandom();
    
        public String nextPassword() {
            return new BigInteger(130, random).toString(32);
        }
    }
    

    P.S.

    I want to give a clear example of how using UUID as a security token may lead to issues:

    In uuid-random we discovered an enormous speed-boost by internally re-using random bytes in a clever way, leading to predictable UUIDs. Though we did not release the change, the RFC allows it and such optimizations could sneak into your UUID library unnoticed.