random

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: BSD-3-Clause Imports: 8 Imported by: 15 modules, 19 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

package random provides methods for generating random strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes added in v1.1.0

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

Bytes returns 'n' bytes read from `crypto/rand`. Unlike `String` the entropy is exactly 8 bits per byte. 'n' must be greater than 0 and no larger than 1048576. It is safe for concurrent use.

func String

func String(opts *Options) (string, error)

String returns a randomly-generated string configured by 'opts'. If 'opts' is nil the result of DefaultOptions is used. It returns an error if neither `Length` nor `Chars` is greater than 0, or if either is larger than 1048576. It is safe for concurrent use.

func Token added in v1.1.0

func Token(bits int) (string, error)

Token returns a URL-safe (unpadded base64) string encoding at least 'bits' bits of entropy from `crypto/rand`. Prefer it over `String` for secrets, API keys and session identifiers. 'bits' must be greater than 0 and no larger than 8388608. It is safe for concurrent use.

Types

type Options

type Options struct {
	// The length in bytes of the final string. Note that most of the default
	// alphabet is made up of multi-byte characters so a string of 'Length' bytes
	// contains far fewer characters (and far less entropy) than an ASCII or
	// alphanumeric string of the same length. Use `ASCII` or `AlphaNumeric` for secrets.
	Length int
	// The number of characters in the final string. If greater than 0 it takes precedence over `Length`.
	Chars int
	// A boolean flag indicating that only ASCII characters be included in the final string.
	ASCII bool
	// A boolean flag indicating that only alphanumeric (a-zA-Z0-9) characters be included in the final string.
	AlphaNumeric bool
	// A boolean flag indicating that only emoji be included in the final string. It can not be combined
	// with `ASCII` or `AlphaNumeric`. Every emoji is 4 bytes long so `Length` must be at least 4 and is
	// rounded down to a multiple of 4.
	Emoji bool
	// A boolean flag indicating the random string should be returned as a base32 encoded string.
	Base32 bool
}

Options is a struct containing configuration options for the `String` method.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns an `Options` instance with no limits or restrictions save a minimum length of 32 bytes.

Jump to

Keyboard shortcuts

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