randchar

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: Apache-2.0 Imports: 4 Imported by: 3

Documentation

Overview

Package randchar helps to generate random sequences of characters from a configured character set, which can be useful for e.g. generating passwords.

It supports many different character sets and configuration options to meet even the weirdest of password requirements. However, note that the strongest passwords are the ones with the least requirements. Imposing e.g. a minimum requirement on generated passwords reduces the entropy of the resulting password. So when possible, avoid using any constraints.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Numeric defines a character set containing all numbers.
	Numeric = NewCharset("0123456789")
	// Lowercase defines a character set containing all lowercase letters.
	Lowercase = NewCharset("abcdefghijklmnopqrstuvwxyz")
	// Uppercase defines a character set containing all uppercase letters.
	Uppercase = NewCharset("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	// Letters defines a character set containing all upper- and lowercase letters of the alphabet.
	Letters = Lowercase.Add(Uppercase)
	// Alphanumeric defines a character set containing letters and numbers.
	Alphanumeric = Letters.Add(Numeric)
	// Symbols defines a character set containing special characters commonly used for passwords.
	Symbols = NewCharset("!@#$%^*-_+=.,?")
	// All defines a character set containing both alphanumeric and symbol characters.
	All = Alphanumeric.Add(Symbols)
	// Similar defines a character set containing similar looking characters.
	Similar = NewCharset("iIlL1oO0")

	// DefaultRand defines the default random generator to use. You can create
	// your own generators using NewRand.
	DefaultRand = MustNewRand(Alphanumeric)
)

Functions

This section is empty.

Types

type Charset added in v0.21.0

type Charset struct {
	// contains filtered or unexported fields
}

Charset is a set of unique characters.

func NewCharset added in v0.21.0

func NewCharset(characters string) Charset

NewCharset creates a set of characters from a given byte slice, removing duplicates to ensure the random generators are not biased.

func (Charset) Add added in v0.21.0

func (c Charset) Add(set Charset) Charset

Add merges two character sets into one, removing duplicates.

func (Charset) Equals added in v0.21.0

func (c Charset) Equals(other Charset) bool

Equals returns true when both character sets contain the exactly same characters.

func (Charset) IsSubset added in v0.21.0

func (c Charset) IsSubset(of Charset) bool

IsSubset returns true when the character set is a subset of the given set. When both sets are the same it returns true too.

func (Charset) Size added in v0.21.0

func (c Charset) Size() int

Size returns the number of distinct characters in the set.

func (Charset) Subtract added in v0.21.0

func (c Charset) Subtract(set Charset) Charset

Subtract removes all characters from a set that match a given set of characters.

type Generator

type Generator interface {
	Generate(n int) ([]byte, error)
}

Generator generates random byte arrays.

func NewGenerator

func NewGenerator(useSymbols bool) Generator

NewGenerator is a shorthand function to create a new random alphanumeric generator, optionally configured to use symbols too. For more flexibility to configure the random generator, use NewRand instead.

type Option added in v0.21.0

type Option func(r Rand) (Rand, error)

Option defines a configuration option for a random reader.

func Min added in v0.21.0

func Min(n int, charset Charset) Option

Min ensures the generated slice contains at least n characters from the given character set. When multiple Min options are given with the same character set, the biggest minimum takes precedence.

func WithReader added in v0.21.0

func WithReader(reader io.Reader) Option

WithReader allows you to set the reader used as source of randomness. Do not use this unless you know what you're doing. By default, the source of randomness is set to crypto/rand.Reader.

type Rand added in v0.21.0

type Rand struct {
	// contains filtered or unexported fields
}

Rand helps generating slices of randomly chosen characters from a given character set.

func MustNewRand added in v0.21.0

func MustNewRand(base Charset, options ...Option) Rand

MustNewRand is a utility function for creating random character generators, which panics upon error so be careful. For more safety, use NewRand instead.

func NewRand added in v0.21.0

func NewRand(base Charset, options ...Option) (Rand, error)

NewRand initializes a new random generator from the given character set and configures it with the given options.

func (Rand) Generate added in v0.21.0

func (r Rand) Generate(n int) ([]byte, error)

Generate returns a randomly generated slice of characters that meets the requirements of the reader.

Directories

Path Synopsis
Package fakes provides mock implementations to be used in testing.
Package fakes provides mock implementations to be used in testing.

Jump to

Keyboard shortcuts

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