rand

package
v0.3.18 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package rand provides cryptographically secure, high-performance random string and byte generation.

Index

Constants

View Source
const (
	Digits    = "0123456789"
	Lowercase = "abcdefghijklmnopqrstuvwxyz"
	Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Symbols   = "!@#$%^&*-_=+"
)

Variables

This section is empty.

Functions

func RandomBytes added in v0.3.18

func RandomBytes(n int) ([]byte, error)

RandomBytes generates n cryptographically secure random bytes using the default alphanumeric character set.

func RandomString added in v0.3.18

func RandomString(n int) (string, error)

RandomString generates a cryptographically secure random string of length n using the default alphanumeric character set.

Types

type Generator added in v0.3.18

type Generator interface {
	// RandBytes generates a random byte slice of the specified size using the generator's character set.
	RandBytes(size int) ([]byte, error)
	// RandString generates a random string of the specified size using the generator's character set.
	RandString(size int) (string, error)
	// Read populates the given byte slice with random bytes from the generator's character set.
	// It implements the io.Reader interface.
	Read(p []byte) (n int, err error)
}

Generator defines the interface for generating cryptographically secure random data.

func CustomRand

func CustomRand(charset string) Generator

CustomRand creates a new random data generator with a custom character set. It returns the RandGenerator interface.

func NewRand

func NewRand(kind Kind) Generator

NewRand creates a new random data generator for the given kind of character set. It returns the RandGenerator interface.

type Kind

type Kind int

Kind defines the character set to be used for random string generation.

const (
	KindDigit     Kind = 1 << iota // KindDigit represents digit characters (0-9).
	KindLowerCase Kind = 1 << iota // KindLowerCase represents lowercase letters (a-z).
	KindUpperCase Kind = 1 << iota // KindUpperCase represents uppercase letters (A-Z).
	KindSymbol    Kind = 1 << iota // KindSymbol represents common symbol characters.

	KindAlphanumeric   = KindDigit | KindLowerCase | KindUpperCase // KindAlphanumeric represents a combination of digits, lowercase, and uppercase letters.
	KindAllWithSymbols = KindAlphanumeric | KindSymbol             // KindAllWithSymbols represents a combination of digits, lowercase, uppercase letters, and symbols.
)

Jump to

Keyboard shortcuts

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