Zum Inhalt springen

Weak Random (ARCH5115)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key go.security.weak-random · Severity warning · Category security · CWE CWE-338

math/rand is non-cryptographic and predictable; using it for security values like tokens, keys, salts, or session ids lets attackers predict them.

func token() string {
b := make([]byte, 16)
for i := range b {
b[i] = byte(rand.Intn(256)) // math/rand
}
return hex.EncodeToString(b)
}
func token() (string, error) {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil { // crypto/rand
return "", err
}
return hex.EncodeToString(b), nil
}

Use crypto/rand for all security-sensitive values; math/rand is only for non-security purposes.