random

package
v9.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package random provides cryptographically secure random string generation in hex, base32, and base64 encodings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Element

func Element[T any](s []T) T

Element fetches a random element from a slice. It returns the zero value of T for an empty slice rather than panicking.

The choice comes from crypto/rand, not math/rand: this package is documented as cryptographically secure, and a caller reaching for it to pick a token, a shard, or a decoy from a slice has every reason to read that promise as covering this function too. crypto/rand.Int cannot fail on any platform Go supports, so the error it returns is discarded rather than widening this signature for a case that does not occur.

func GenerateBase32EncodedString

func GenerateBase32EncodedString(ctx context.Context, length int) (string, error)

GenerateBase32EncodedString generates a one-off value with an anonymous Generator.

func GenerateBase64EncodedString

func GenerateBase64EncodedString(ctx context.Context, length int) (string, error)

GenerateBase64EncodedString generates a one-off value with an anonymous Generator.

func GenerateHexEncodedString

func GenerateHexEncodedString(ctx context.Context, length int) (string, error)

GenerateHexEncodedString generates a one-off value with an anonymous Generator.

Example
package main

import (
	"context"
	"fmt"

	"github.com/primandproper/platform-go/v9/random"
)

func main() {
	s, err := random.GenerateHexEncodedString(context.Background(), 16)
	if err != nil {
		panic(err)
	}

	// Output is non-deterministic, but always 32 hex characters (16 bytes * 2)
	fmt.Println(len(s))
}
Output:
32

func GenerateRawBytes

func GenerateRawBytes(ctx context.Context, length int) ([]byte, error)

GenerateRawBytes generates a one-off value with an anonymous Generator.

Example
package main

import (
	"context"
	"fmt"

	"github.com/primandproper/platform-go/v9/random"
)

func main() {
	b, err := random.GenerateRawBytes(context.Background(), 8)
	if err != nil {
		panic(err)
	}

	fmt.Println(len(b))
}
Output:
8

func MustGenerateHexEncodedString

func MustGenerateHexEncodedString(ctx context.Context, length int) string

MustGenerateHexEncodedString generates a one-off value with an anonymous Generator.

func MustGenerateRawBytes

func MustGenerateRawBytes(ctx context.Context, length int) []byte

MustGenerateRawBytes generates a one-off value with an anonymous Generator, but does not return an error.

func RegisterGenerator

func RegisterGenerator(i do.Injector)

RegisterGenerator registers a Generator with the injector.

Types

type Generator

type Generator interface {
	GenerateHexEncodedString(ctx context.Context, length int) (string, error)
	GenerateBase32EncodedString(context.Context, int) (string, error)
	GenerateBase64EncodedString(context.Context, int) (string, error)
	GenerateRawBytes(context.Context, int) ([]byte, error)
}

Generator should generate random strings securely.

func NewGenerator

func NewGenerator(opts ...Option) Generator

NewGenerator builds a new Generator.

type Option

type Option func(*options)

Option configures the Generator this package constructs. The zero configuration works: an absent logger logs nowhere and an absent tracer provider traces nowhere.

func WithLogger

func WithLogger(logger logging.Logger) Option

WithLogger attaches a logger.

func WithTracerProvider

func WithTracerProvider(tracerProvider tracing.TracerProvider) Option

WithTracerProvider attaches a tracer provider, enabling spans on every operation.

Directories

Path Synopsis
Package randommock provides moq-generated mock implementations of the random package's interfaces.
Package randommock provides moq-generated mock implementations of the random package's interfaces.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL