Documentation ¶
Overview ¶
Package rndstring provides an interface and generators to generate various sorts of random strings (or tokens) using the native crypto/rand cryptographically strong random number generator if available. If not available it will use a fall back random number generator.
Index ¶
- func Generate() string
- func Join(delim string, generators ...StringGenerator) string
- func RandomAPIToken() string
- func RandomBytes(N int) []byte
- func RandomIPv4() string
- func RandomPassword() string
- func RandomString(N int) string
- func ReadBytes(buf []byte)
- func ReadBytesFallback(buf []byte)
- func ReadBytesNoFallback(buf []byte)
- func RegisterStringGenerator(name string, f NewStringGeneratorF)
- func SetDefaultStringGenerator(tg StringGenerator)
- func StringGenerators() []string
- type NewStringGeneratorF
- type StringGenerator
- func NewASCIIGenerator(N int) (StringGenerator, error)
- func NewAlphabetGenerator(N int, alphabet []rune) (StringGenerator, error)
- func NewBase32Generator(N int) (StringGenerator, error)
- func NewBase64Generator(N int) (StringGenerator, error)
- func NewBase64URLGenerator(N int) (StringGenerator, error)
- func NewDigitsGenerator(N int) (StringGenerator, error)
- func NewDummyGenerator(N int) (StringGenerator, error)
- func NewHexGenerator(N int) (StringGenerator, error)
- func NewHexStrGenerator(N int) (StringGenerator, error)
- func NewLettersDigitsGenerator(N int) (StringGenerator, error)
- func NewLettersGenerator(N int) (StringGenerator, error)
- func NewLettersSymbolsDigitsGenerator(N int) (StringGenerator, error)
- func NewLettersSymbolsGenerator(N int) (StringGenerator, error)
- func NewLowerCaseDigitsGenerator(N int) (StringGenerator, error)
- func NewLowerCaseGenerator(N int) (StringGenerator, error)
- func NewLowerCaseSymbolsGenerator(N int) (StringGenerator, error)
- func NewStringGenerator(name string, N int) (StringGenerator, error)
- func NewSymbolsGenerator(N int) (StringGenerator, error)
- func NewUpperCaseDigitsGenerator(N int) (StringGenerator, error)
- func NewUpperCaseGenerator(N int) (StringGenerator, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
func Generate() string
Generate a string using the default StringGenerator. See `SetDefaultStringGenerator`. Do not call this without having set a default StringGenerator.
func Join ¶
func Join(delim string, generators ...StringGenerator) string
Join joins the strings generated by the generators together using the specified delimiter.
func RandomAPIToken ¶
func RandomAPIToken() string
RandomAPIToken returns a random API token. If you want to generate random API tokens this is the way to go. The length of the returned string is 24 which amounts to roughly 143bits. It contains only letters and digits.
func RandomPassword ¶
func RandomPassword() string
RandomPassword returns a random password. If you want to generate random passwords this is the way to go. The length of the returned string is 18.
func RandomString ¶
RandomString returns a random string of length N consisting of letters, digits and symbols. If you just want to generate a random ascii string this is the easiest way.
func ReadBytesFallback ¶
func ReadBytesFallback(buf []byte)
ReadBytesFallback reads len(buf) random bytes using the fall-back method.
func ReadBytesNoFallback ¶
func ReadBytesNoFallback(buf []byte)
ReadBytesNoFallback reads len(buf random bytes without using fall-back method.
func RegisterStringGenerator ¶
func RegisterStringGenerator(name string, f NewStringGeneratorF)
RegisterStringGenerator registers a StringGenerator. This panics if there's already one registered under the specified name.
func SetDefaultStringGenerator ¶
func SetDefaultStringGenerator(tg StringGenerator)
SetDefaultStringGenerator sets the (global) default generator.
func StringGenerators ¶
func StringGenerators() []string
StringGenerators returns the names of all available string generators.
Types ¶
type NewStringGeneratorF ¶
type NewStringGeneratorF func(int) (StringGenerator, error)
NewStringGeneratorF is a wrapper type around a function constructing StringGenerators. Negative lengths will result in empty strings.
type StringGenerator ¶
type StringGenerator interface { // Generates and returns a new string. Is safe // for concurrent use. Generate() string }
A StringGenerator generates random (string) strings using a cryptographically strong random number generator.
func NewASCIIGenerator ¶
func NewASCIIGenerator(N int) (StringGenerator, error)
NewASCIIGenerator is just an alias for NewLettersSymbolsDigitsGenerator
func NewAlphabetGenerator ¶
func NewAlphabetGenerator(N int, alphabet []rune) (StringGenerator, error)
NewAlphabetGenerator returns a new string generator returning strings of length N using the alphabet provided. The alphabet must not be larger than 255 runes.
func NewBase32Generator ¶
func NewBase32Generator(N int) (StringGenerator, error)
NewBase32Generator returns a new string generator returning N bytes base32 encoded. (Thus the size of the returned string string is longer than N).
func NewBase64Generator ¶
func NewBase64Generator(N int) (StringGenerator, error)
NewBase64Generator returns a new string generator returning N bytes base64 encoded. (Thus the size of the returned string string is longer than N).
func NewBase64URLGenerator ¶
func NewBase64URLGenerator(N int) (StringGenerator, error)
NewBase64URLGenerator returns a new string generator returning N byte base64-url encoded WITHOUT padding. (Thus the size of the returned string string is longer than N).
func NewDigitsGenerator ¶
func NewDigitsGenerator(N int) (StringGenerator, error)
NewDigitsGenerator returns a new string generator returning strings of length N consisting of digits.
func NewDummyGenerator ¶
func NewDummyGenerator(N int) (StringGenerator, error)
NewDummyGenerator returns a new string generator returning strings of length N. This just repeats the letter A. Don't use this in production!
func NewHexGenerator ¶
func NewHexGenerator(N int) (StringGenerator, error)
NewHexGenerator returns a new string generator returning N bytes hex encoded. (Thus the size of the returned string string is N*2).
func NewHexStrGenerator ¶
func NewHexStrGenerator(N int) (StringGenerator, error)
NewHexStrGenerator returns a new string generator returning strings of exactly length N consisting of 0-9 and a-f.
func NewLettersDigitsGenerator ¶
func NewLettersDigitsGenerator(N int) (StringGenerator, error)
NewLettersDigitsGenerator returns a new string generator returning strings of length N consisting of letters and digits.
func NewLettersGenerator ¶
func NewLettersGenerator(N int) (StringGenerator, error)
NewLettersGenerator returns a new string generator returning strings of length N consisting of letters.
func NewLettersSymbolsDigitsGenerator ¶
func NewLettersSymbolsDigitsGenerator(N int) (StringGenerator, error)
NewLettersSymbolsDigitsGenerator returns a new string generator returning strings of length N consisting of letters, symbols and digits.
func NewLettersSymbolsGenerator ¶
func NewLettersSymbolsGenerator(N int) (StringGenerator, error)
NewLettersSymbolsGenerator returns a new string generator returning strings of length N consisting of letters and symbols.
func NewLowerCaseDigitsGenerator ¶
func NewLowerCaseDigitsGenerator(N int) (StringGenerator, error)
NewLowerCaseDigitsGenerator returns a new string generator returning strings of length N consisting of lower case letters and digits.
func NewLowerCaseGenerator ¶
func NewLowerCaseGenerator(N int) (StringGenerator, error)
NewLowerCaseGenerator returns a new string generator returning strings of length N consisting of lower case letters.
func NewLowerCaseSymbolsGenerator ¶
func NewLowerCaseSymbolsGenerator(N int) (StringGenerator, error)
NewLowerCaseSymbolsGenerator returns a new string generator returning strings of length N consisting of lower case letters and symbols.
func NewStringGenerator ¶
func NewStringGenerator(name string, N int) (StringGenerator, error)
NewStringGenerator returns a new StringGenerator by name and length. Length may either refer to the total length of the string or the amount of bytes it encodes (this is for example the case when using base32, base64 or hex).
func NewSymbolsGenerator ¶
func NewSymbolsGenerator(N int) (StringGenerator, error)
NewSymbolsGenerator returns a new string generator returning strings of length N consisting of symbols.
func NewUpperCaseDigitsGenerator ¶
func NewUpperCaseDigitsGenerator(N int) (StringGenerator, error)
NewUpperCaseDigitsGenerator returns a new string generator returning strings of length N consisting of upper case letters and digits.
func NewUpperCaseGenerator ¶
func NewUpperCaseGenerator(N int) (StringGenerator, error)
NewUpperCaseGenerator returns a new string generator returning strings of length N consisting of upper case letters.