26

Generate secrets with cryptographically secure RNG!

Can you find the security problem in this Go snippet?

import "math/rand"
func generateAES256Key() ([]byte, error) {
    var key = make([]byte, 32)
    if _, err := rand.Read(key); err != nil {
        return nil, err
    }
    return key, nil
}
When generating random values for cryptographic algorithms (keys, nonces etc.), always generate them with a cryptographically secure random number generator (CSPRNG). Usually math libraries (like Go’s math/rand) provide a RNG implementation that is optimized for performance rather than security. Thus, random numbers generated with these libraries are usually not suited for cryptographic operations. Instead always use RNGs which explicitly state that they implement a CSPRNG like Go’s crypto/rand module.
Icon with a waving hand

Get in touch

sigma star gmbh
Eduard-Bodem-Gasse 6, 1st floor
6020 Innsbruck | Austria

sigma star gmbh logo