﻿# V5904\. OWASP\. A cryptographically weak pseudo\-random number generator is used in security\-sensitive code\. Consider using a stronger alternative\.

The analyzer has detected the use of a cryptographically weak pseudorandom number generator \(PRNG\) in the security\-sensitive code\.

Vulnerabilities related to the use of predictable PRNG can be classified under the following categories in the OWASP Top 10 Application Security Risks 2025:

* [A04:2025 Cryptographic Failures](https://top10.owasp.org/2025/A04_2025-Cryptographic_Failures/)
* [A07:2025 Authentication Failures](https://top10.owasp.org/2025/A07_2025-Authentication_Failures/)

The example:

```cpp
import (
  ....
  "math/rand"
  ....
)

func ResetPassword(...., r *http.Request) {
  ....
  token := fmt.Sprintf("%d", rand.Int())
  userEmail := r.URL.Query().Get("email")

  _, err := DB.Exec("INSERT INTO password_resets " +
                    "(email, token) VALUES (?, ?)",
                    userEmail, 
                    token)  
  ....
}
```

In the example, the function from the `[math/rand](https://pkg.go.dev/math/rand)` package is used to generate the password reset token\. The algorithms in this package are deterministic and predictable, which allows an attacker to generate or guess the token\.

To generate secret keys, session tokens, passwords, and other sensitive data, it would be recommended to use a cryptographically secure random number generator from the `[crypto/rand](https://pkg.go.dev/crypto/rand)` package:

```cpp
import (
  ....
  secrand "crypto/rand"
  "math/big"
  ....
)
....
func ResetPassword(....) {
  ....
  randVal, _ := secrand.Int(secrand.Reader, ....)
  token := fmt.Sprintf("%d", randVal.Int64())
  ....
}
```

For more security recommendations, see the OWASP Cheat Sheet Series:

* [Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)
* [Cryptographic Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html)