Documentation
¶
Overview ¶
Package crand implements a cryptographically secure random number generator.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Reader io.Reader = &R{}
Reader is a global, shared instance of a cryptographically secure random number generator. It is safe for concurrent use.
- On Linux, FreeBSD, and Dragonfly, uses getrandom(2).
- On macOS, NetBSD, and OpenBSD, uses arc4random_buf(3).
Functions ¶
func Read ¶
Read fills b with cryptographically secure random bytes. It never returns an error, and always fills b entirely. Uses arc4random_buf on macOS/BSD and getrandom on Linux.
Example ¶
package main
import (
"solod.dev/so/crypto/crand"
)
func main() {
// Note that no error handling is necessary, as Read always succeeds.
key := make([]byte, 32)
crand.Read(key)
_ = key
}
Output:
func Text ¶
Text returns a cryptographically random string using the standard RFC 4648 base32 alphabet for use when a secret string, token, password, or other text is needed. The result contains at least 128 bits of randomness, enough to prevent brute force guessing attacks and to make the likelihood of collisions vanishingly small. A future version may return longer texts as needed to maintain those properties.
Requires a buffer of 26 bytes (⌈log₃₂ 2¹²⁸⌉ = 26 chars).
Example ¶
package main
import (
"solod.dev/so/crypto/crand"
"solod.dev/so/fmt"
)
func main() {
buf := make([]byte, 26)
key := crand.Text(buf)
// The key is base32 and safe to display.
fmt.Println(key)
}
Output: