Weak pseudorandom number generation High

Insufficiently random generators or hardcoded seeds can make pseudorandom sequences predictable, which may lead to security vulnerabilities.

Detector ID
kotlin/weak-random-number-generation@v1.0
Category

Noncompliant example

1// Noncompliant: `Random()` is not a secure random number generator
2fun noncompliant() {
3    val random = Random() 
4    val bytes = ByteArray(20)
5    random.nextBytes(bytes)
6}

Compliant example

1// Compliant: Using `SecureRandom()` to generate random numbers
2fun compliant() {
3    val random = SecureRandom()
4    val bytes = ByteArray(20)
5    random.nextBytes(bytes)
6}