Documentation
¶
Overview ¶
Package randomstring generates random strings for use as identifiers, tokens, passwords, and anywhere else you need short, configurable random text.
The core type is Randomizer, configured through exported struct fields:
r := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigits,
Length: 13,
Unique: true,
}
id, err := r.GenerateOne()
The package also provides ready-made character sets (LowerLetters, UpperLetters, Digits, Symbols, and their common combinations) that can be passed as a Randomizer's Universe.
Index ¶
Examples ¶
Constants ¶
const Digits = "0123456789"
Digits contains the decimal digits 0-9.
const LowerLetters = "abcdefghijklmnopqrstuvwxyz"
LowerLetters contains the lowercase ASCII letters a-z.
const LowerUpperDigits = LowerLetters + UpperLetters + Digits
LowerUpperDigits contains all lowercase and uppercase ASCII letters plus digits.
const LowerUpperDigitsSymbols = LowerUpperDigits + Symbols
LowerUpperDigitsSymbols contains all lowercase and uppercase ASCII letters, digits, and symbols.
const LowerUpperLetters = LowerLetters + UpperLetters
LowerUpperLetters contains all lowercase and uppercase ASCII letters.
const Symbols = "!@#$%&*()-_+={};:.,"
Symbols contains a set of common punctuation and symbol characters.
const UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
UpperLetters contains the uppercase ASCII letters A-Z.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Randomizer ¶
type Randomizer struct {
// Universe is the set of runes from which generated strings are drawn.
// It must not be empty. Multi-byte (Unicode) runes are supported.
Universe string
// Length is the number of runes in each generated string. It must be
// greater than zero.
Length int
// Unique, when true, makes a single call to Generate return amount
// distinct strings. It has no effect on GenerateOne.
Unique bool
// Secure, when true, draws randomness from crypto/rand, producing output
// suitable for passwords and other security-sensitive tokens. Secure
// generation ignores Seed.
Secure bool
// Seed makes output deterministic: two Randomizers with the same
// configuration and the same non-zero Seed produce identical output.
// The zero value (the default) seeds from a random source. Seed is
// ignored when Secure is true.
Seed int64
}
Randomizer generates random strings according to its configuration.
A Randomizer value may be reused and shared across goroutines: all fields are read only during generation, and no mutable state is kept on the value.
func (Randomizer) Generate ¶
func (r Randomizer) Generate(amount int) ([]string, error)
Generate generates amount random strings.
If Unique is set, the returned strings are all distinct. Generate returns an error if Universe is empty, Length is less than one, amount is negative, or Unique is set and amount exceeds the number of possible permutations.
Example ¶
package main
import (
"fmt"
"github.com/gbbocchini/go-randomstring"
)
func main() {
r := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigits,
Length: 8,
Unique: true,
}
ids, err := r.Generate(1000)
if err != nil {
panic(err)
}
fmt.Println(len(ids))
}
Output: 1000
func (Randomizer) GenerateOne ¶
func (r Randomizer) GenerateOne() (string, error)
GenerateOne generates a single random string.
It returns an error if Universe is empty or Length is less than one.
Example ¶
package main
import (
"fmt"
"github.com/gbbocchini/go-randomstring"
)
func main() {
r := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigits,
Length: 13,
Seed: 1,
}
id, err := r.GenerateOne()
if err != nil {
panic(err)
}
fmt.Println(id)
}
Output: 9g14r5YgIsx9v
Example (Secure) ¶
package main
import (
"fmt"
"github.com/gbbocchini/go-randomstring"
)
func main() {
r := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigitsSymbols,
Length: 32,
Secure: true,
}
token, err := r.GenerateOne()
if err != nil {
panic(err)
}
fmt.Println(len(token))
}
Output: 32
func (Randomizer) UniquePermutations ¶
func (r Randomizer) UniquePermutations() *big.Int
UniquePermutations returns the maximum number of distinct strings this Randomizer can produce, as a big.Int.
It is the number of distinct runes in Universe raised to the power of Length. When Universe contains no duplicate runes this equals len(Universe)^Length.