Documentation
¶
Overview ¶
Package securerandom is a pure-Go (no cgo) reimplementation of Ruby's SecureRandom module — the formatting layer MRI 4.0.5 layers over a cryptographically secure entropy source.
The randomness comes from an injectable RandSource (the default is crypto/rand); everything else — hex, base64, URL-safe base64, UUIDs, the alphanumeric/choose string builder, and the random_number distribution — is deterministic, interpreter-independent formatting, so it lives here as pure Go. The hex path is accelerated with github.com/go-simd/hex and the base64 paths with github.com/go-simd/base64; both are byte-identical drop-ins for the standard library, so the output matches MRI exactly.
Because the default source is cryptographically random, callers (and the tests) assert on the FORMAT, length, charset, and UUID bit-layout, not on exact bytes. Feed a fixed RandSource when a deterministic result is needed.
Index ¶
- func Alphanumeric(n int, chars ...string) string
- func Base64(n ...int) string
- func Hex(n ...int) string
- func RandomBytes(n ...int) []byte
- func RandomFloat() float64
- func RandomInt(n int64) int64
- func RandomNumber(n ...float64) (int64, float64, bool)
- func UrlsafeBase64(n int, padding bool) string
- func Uuid() string
- func UuidV7() string
- type RandSource
- type SecureRandom
- func (s *SecureRandom) Alphanumeric(n int, chars ...string) string
- func (s *SecureRandom) Base64(n ...int) string
- func (s *SecureRandom) Hex(n ...int) string
- func (s *SecureRandom) RandomBytes(n ...int) []byte
- func (s *SecureRandom) RandomFloat() float64
- func (s *SecureRandom) RandomInt(n int64) int64
- func (s *SecureRandom) RandomNumber(n ...float64) (i int64, f float64, isInt bool)
- func (s *SecureRandom) UrlsafeBase64(n int, padding bool) string
- func (s *SecureRandom) Uuid() string
- func (s *SecureRandom) UuidV7() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Alphanumeric ¶
Alphanumeric calls SecureRandom.Alphanumeric on the default source.
func RandomBytes ¶
RandomBytes calls SecureRandom.RandomBytes on the default source.
func RandomFloat ¶
func RandomFloat() float64
RandomFloat calls SecureRandom.RandomFloat on the default source.
func RandomInt ¶
RandomInt calls SecureRandom.RandomInt on the default source.
func RandomNumber ¶
RandomNumber calls SecureRandom.RandomNumber on the default source.
func UrlsafeBase64 ¶
UrlsafeBase64 calls SecureRandom.UrlsafeBase64 on the default source.
Types ¶
type RandSource ¶
RandSource is the entropy seam SecureRandom draws from. The default is crypto/rand (via DefaultSource); a fixed implementation makes the formatters deterministic for testing. Read fills p with len(p) random bytes and returns an error only on a broken environment.
var DefaultSource RandSource = cryptoSource{}
DefaultSource is the package-level entropy source used by the top-level functions. It defaults to crypto/rand. Replace it (and restore it) to make the formatters deterministic; the per-call API on SecureRandom is preferred for that, as it is concurrency-safe.
type SecureRandom ¶
type SecureRandom struct {
// contains filtered or unexported fields
}
SecureRandom is a SecureRandom generator bound to a specific entropy source. The zero value is not usable; build one with New. Every method mirrors the matching MRI 4.0.5 SecureRandom method.
func New ¶
func New(src RandSource) *SecureRandom
New returns a SecureRandom drawing from src. A nil src uses crypto/rand.
func (*SecureRandom) Alphanumeric ¶
func (s *SecureRandom) Alphanumeric(n int, chars ...string) string
Alphanumeric returns an n-character (default 16) random string. With no chars it draws from MRI's default A-Z a-z 0-9 alphabet; pass an explicit chars set to draw from it instead. This is MRI's SecureRandom.alphanumeric, and it uses the same #choose construction so the character distribution matches.
func (*SecureRandom) Base64 ¶
func (s *SecureRandom) Base64(n ...int) string
Base64 returns the standard padded base64 of n random bytes (default 16). This is MRI's SecureRandom.base64; the encoding is the SIMD drop-in for base64.StdEncoding.
func (*SecureRandom) Hex ¶
func (s *SecureRandom) Hex(n ...int) string
Hex returns 2*n hexadecimal characters built from n random bytes (default 16, so 32 chars). This is MRI's SecureRandom.hex; the encoding is the SIMD drop-in for encoding/hex.
func (*SecureRandom) RandomBytes ¶
func (s *SecureRandom) RandomBytes(n ...int) []byte
RandomBytes returns n random bytes (default 16) as a []byte. This is MRI's SecureRandom.random_bytes; in Ruby the result is an ASCII-8BIT (binary) String of bytesize n, which maps to a Go byte slice.
func (*SecureRandom) RandomFloat ¶
func (s *SecureRandom) RandomFloat() float64
RandomFloat returns a uniform random float64 in [0, 1), built from 53 random bits exactly as MRI's Random#random_number with no bound. This is the zero-argument branch of SecureRandom.random_number.
func (*SecureRandom) RandomInt ¶
func (s *SecureRandom) RandomInt(n int64) int64
RandomInt returns a uniform random integer in [0, n) for n > 0, and 0 for n <= 0. This is the Integer branch of MRI's SecureRandom.random_number(n).
func (*SecureRandom) RandomNumber ¶
func (s *SecureRandom) RandomNumber(n ...float64) (i int64, f float64, isInt bool)
RandomNumber mirrors MRI's SecureRandom.random_number. With no argument, or a non-positive one, it returns a Float in [0, 1) (as the second return). With a positive integer it returns an Integer in [0, n) (first return, isInt true). With a positive float it returns a Float in [0, n). Callers that know the argument type may prefer SecureRandom.RandomFloat / SecureRandom.RandomInt.
func (*SecureRandom) UrlsafeBase64 ¶
func (s *SecureRandom) UrlsafeBase64(n int, padding bool) string
UrlsafeBase64 returns the URL- and filename-safe base64 of n random bytes (default 16). MRI omits the padding by default; pass padding=true to keep it. This is MRI's SecureRandom.urlsafe_base64.
func (*SecureRandom) Uuid ¶
func (s *SecureRandom) Uuid() string
Uuid returns a random RFC 4122 version-4 UUID, the xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx form where the version nibble is 4 and the variant nibble y is one of 8, 9, a, b. This is MRI's SecureRandom.uuid.
func (*SecureRandom) UuidV7 ¶
func (s *SecureRandom) UuidV7() string
UuidV7 returns a random UUID version 7: a 48-bit big-endian Unix-milliseconds timestamp followed by random bits, with the version nibble 7 and the 10x variant. This is MRI 4.0's SecureRandom.uuid_v7. The timestamp comes from the injected clock (default: time.Now) so it is deterministic under test.
