go-ruby-securerandom

Ruby's SecureRandom in pure Go β€” MRI-compatible, SIMD-accelerated, no cgo.

pure Go Β· zero cgo MRI SecureRandom hex Β· base64 Β· url-safe UUID v4 & v7 alphanumeric Β· random_number go-simd/hex + base64 SIMD on 6 arches injectable RandSource 100% coverage 6 arches
Documentation GitHub
Documentation (MkDocs Material + mike) License: BSD-3-Clause Go 1.26.4+ Coverage 100%

go-ruby-securerandom is a pure-Go (no cgo) reimplementation of Ruby's SecureRandom β€” the formatting layer MRI 4.0.5 builds over a cryptographically secure entropy source. Hex, Base64, UrlsafeBase64, RandomBytes, Uuid / UuidV7, Alphanumeric and RandomNumber all produce byte-identical output to MRI. The randomness comes from an injectable RandSource (default crypto/rand); everything else is deterministic, interpreter-independent formatting. The hex path runs on go-simd/hex and the base64 paths on go-simd/base64 β€” a CGO=0 SIMD path across all six 64-bit Go arches. It is the SecureRandom backend for go-embedded-ruby, a standalone reusable module bound by rbgo just like go-ruby-regexp, go-ruby-erb and go-ruby-securerandom β€” differential-tested against MRI, 100% coverage, CI green across 6 arches and 3 OSes.

Encoders ready

Hex(n), Base64(n), UrlsafeBase64(n, padding) and RandomBytes(n) with MRI’s defaults (n = 16; URL-safe base64 unpadded unless asked). Hex is SIMD-accelerated via go-simd/hex; the base64 family via go-simd/base64.

UUIDs (v4 & v7) ready

Uuid() is an RFC 4122 version-4 UUID with the exact xxxxxxxx-xxxx-4xxx-yxxx-… layout (version nibble 4, variant 8/9/a/b). UuidV7() is MRI 4.0’s version-7 UUID: a 48-bit big-endian Unix-milliseconds prefix from an injectable clock plus random bits.

Alphanumeric & random_number ready

Alphanumeric(n, chars…) builds from MRI’s A-Z a-z 0-9 default (or a supplied set) with the same Random::Formatter#choose base-size construction. RandomNumber returns a Float in [0,1) for no/non-positive n, an Integer in [0,n) for a positive integer, a Float in [0,n) for a positive float.

SIMD acceleration ready

The hex path runs on go-simd/hex and the base64 paths on go-simd/base64 β€” go-asmgen kernels that are byte-identical drop-ins for the standard library, a CGO=0 SIMD path across all six 64-bit Go arches.

The RandSource seam ready

Every method draws from an injectable RandSource (default crypto/rand); a fixed source makes the formatters deterministic, which is how the suite asserts exact bytes. Randomness is the only non-deterministic input; all encoding and layout is pure formatting.

Differential oracle & coverage ready

Each method is run under the system ruby and checked for the same format, length, charset and UUID bit-layout (never exact bytes, since both sides draw independent CSPRNGs); 100% coverage, gofmt + go vet clean, green across all six 64-bit Go arches and three OSes.

A faithful port of Ruby's SecureRandom in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. The randomness is an injectable RandSource (default crypto/rand); all encoding and layout β€” hex, base64, URL-safe, the UUID v4/v7 bit-layout, the #choose alphanumeric builder and the random_number distribution β€” is deterministic, pure-Go formatting, with the hex path on go-simd/hex and the base64 paths on go-simd/base64. Validated differentially against the system ruby binary. It is a standalone, reusable module and the SecureRandom backend for the sibling org github.com/go-embedded-ruby.