Some checks are pending
docker-build-cometbft / vars (push) Waiting to run
docker-build-cometbft / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-cometbft / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-cometbft / merge-images (push) Blocked by required conditions
docker-build-e2e-node / vars (push) Waiting to run
docker-build-e2e-node / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-e2e-node / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-e2e-node / merge-images (push) Blocked by required conditions
35 lines
710 B
Go
35 lines
710 B
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
|
|
}
|