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 ¶
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
NewCharset creates a set of characters from a given byte slice, removing duplicates to ensure the random generators are not biased.
func (Charset) Equals ¶ added in v0.21.0
Equals returns true when both character sets contain the exactly same characters.
func (Charset) IsSubset ¶ added in v0.21.0
IsSubset returns true when the character set is a subset of the given set. When both sets are the same it returns true too.
type Generator ¶
Generator generates random byte arrays.
func NewGenerator ¶
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
Option defines a configuration option for a random reader.
func Min ¶ added in v0.21.0
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
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
MustNewRand is a utility function for creating random character generators, which panics upon error so be careful. For more safety, use NewRand instead.