random

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 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 an array.

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/verygoodsoftwarenotvirus/platform/v3/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/verygoodsoftwarenotvirus/platform/v3/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(logger logging.Logger, tracerProvider tracing.TracerProvider) Generator

NewGenerator builds a new Generator.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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