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
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 ¶
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.
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.