Skip to main content

Insecurely generated token


Secure token generation mechanism


  • Usage of Scala for building scalable and high-performance applications
  • Usage of java.util.Random for generating random numbers in Java


Non compliant code

import java.util.Random

def generateToken(): String = {
val random = new Random()
val token = new Array[Byte](20)

The above code is a simple token generation function written in Scala for a backend application that uses the Play framework. The function generateToken() generates a 20-byte long token using the java.util.Random class.

The vulnerability lies in the use of java.util.Random for token generation. This class is not suitable for generating secure tokens as it does not provide a cryptographically strong number generator. The numbers generated by this class can be predicted and hence an attacker can guess the token.

In addition, the byte array is converted to a string using the mkString function. This could potentially leak information about the token if the output string is not properly sanitized before being outputted.

Therefore, the token generated by this function is insecure and can be cracked by an attacker, leading to potential information disclosure.


  • Replace the usage of java.util.Random with a cryptographically secure random number generator.
  • Use a secure cryptographic mechanism, such as a secure hash function, to generate the token.
  • Ensure that the token is sufficiently long and has enough entropy to make it difficult to crack.
  • Consider using a library or framework-provided functionality for generating secure tokens.

Compliant code

import javax.xml.bind.DatatypeConverter

def generateToken(): String = {
val random = new SecureRandom()
val bytes = new Array[Byte](20)

The original code was using java.util.Random to generate a token, which is not cryptographically secure. This means that the tokens it generates can be predictable, especially if the seed used by the Random instance is known or can be guessed.

The updated code uses instead, which is a cryptographically strong random number generator (RNG). It provides a cryptographically strong random number by using a cryptographically strong pseudo-random number generator (CSPRNG).

The SecureRandom class is widely used in cryptographic applications and protocols to generate random numbers. These random numbers are used for generating keys, initialization vectors, and other cryptographic values.

The DatatypeConverter.printHexBinary(bytes) is used to convert the byte array into a string of hexadecimal values. This is a common way to represent binary data as a string, and it's particularly useful for things like tokens where the binary data may not be easily represented as a string.

The size of the byte array (20 bytes) ensures that the token has a sufficient length and entropy to make it difficult to crack. This is because the larger the number of possible tokens, the harder it is for an attacker to guess a valid token.

By using a secure RNG and a sufficiently large token size, the updated code helps to mitigate the risk of token prediction attacks.