Revision e47c43fdd9500e5e42912fa0afcb64934adb625e authored by Sam Kleinman on 30 July 2021, 13:29:00 UTC, committed by GitHub on 30 July 2021, 13:29:00 UTC
2 parent s 4beeebc + 3aec71c
Raw File
random.go
package crypto

import (
	crand "crypto/rand"
	"encoding/hex"
	"io"
)

// This only uses the OS's randomness
func randBytes(numBytes int) []byte {
	b := make([]byte, numBytes)
	_, err := crand.Read(b)
	if err != nil {
		panic(err)
	}
	return b
}

// This only uses the OS's randomness
func CRandBytes(numBytes int) []byte {
	return randBytes(numBytes)
}

// CRandHex returns a hex encoded string that's floor(numDigits/2) * 2 long.
//
// Note: CRandHex(24) gives 96 bits of randomness that
// are usually strong enough for most purposes.
func CRandHex(numDigits int) string {
	return hex.EncodeToString(CRandBytes(numDigits / 2))
}

// Returns a crand.Reader.
func CReader() io.Reader {
	return crand.Reader
}
back to top