randchar

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 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

Examples

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")
	// HumanReadable defines a character set containing all alphanumeric characters except the similar ones.
	HumanReadable = Alphanumeric.Subtract(Similar)

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

Functions

func Generate added in v0.24.0

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

Generate generates a random slice of alphanumeric characters.

Types

type Charset added in v0.21.0

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

Charset is a set of unique characters.

func CharsetByName added in v0.26.0

func CharsetByName(charsetName string) (Charset, bool)

CharsetByName returns the charset with the specified name.

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.

Example

Generate a random slice of 30 alphanumeric characters.

package main

import (
	"log"

	"github.com/secrethub/secrethub-go/pkg/randchar"
)

func main() {
	val, err := randchar.Generate(30)
	if err != nil {
		log.Fatal(err)
	}
	print(string(val))
}
Output:

Example (CombineCharsets)

Generate a 10 character alphanumeric string containing lowercase letters and digits.

package main

import (
	"log"

	"github.com/secrethub/secrethub-go/pkg/randchar"
)

func main() {
	customCharset := randchar.Lowercase.Add(randchar.Numeric)
	rand, err := randchar.NewRand(customCharset)
	if err != nil {
		log.Fatal(err)
	}

	val, err := rand.Generate(10)
	if err != nil {
		log.Fatal(err)
	}
	print(string(val))
}
Output:

Example (CustomCharset)

Generate an 8 character long hexadecimal string.

package main

import (
	"log"

	"github.com/secrethub/secrethub-go/pkg/randchar"
)

func main() {
	hexCharset := randchar.NewCharset("0123456789ABCDEF")
	rand, err := randchar.NewRand(hexCharset)
	if err != nil {
		log.Fatal(err)
	}

	val, err := rand.Generate(8)
	if err != nil {
		log.Fatal(err)
	}
	print(string(val))
}
Output:

Example (WithRules)

Generate a 15 character alphanumeric string with at least 3 symbols, 1 uppercase letter, 1 lowercase letter and 1 digit.

package main

import (
	"log"

	"github.com/secrethub/secrethub-go/pkg/randchar"
)

func main() {
	symbolsRule := randchar.Min(3, randchar.Symbols)
	uppercaseRule := randchar.Min(1, randchar.Uppercase)
	lowercaseRule := randchar.Min(1, randchar.Lowercase)
	numberRule := randchar.Min(1, randchar.Numeric)

	rand, err := randchar.NewRand(randchar.All, symbolsRule, uppercaseRule, lowercaseRule, numberRule)
	if err != nil {
		log.Fatal(err)
	}

	val, err := rand.Generate(15)
	if err != nil {
		log.Fatal(err)
	}
	print(string(val))
}
Output:

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