randomstring: github.com/go-passwd/randomstring Index | Files

package randomstring

import "github.com/go-passwd/randomstring"

Package randomstring provides:

- function `Generate` to generate random string based on set of characters

- type `Generator` to generate random string based on set of rules

Example

This code:

gen := randomstring.New(randomstring.NewLength(20), randomstring.NewInlcudeCharset(randomstring.LowerLetters), randomstring.NewNoDuplicateCharacters())
fmt.Println(gen.Generate())

will generate random string that:

- has length 20

- contains lowercase latin letters

- all letters in random string are unique

Custom rules

Generator accept four types of rules:

- Length rule that defines a random string length

- Charset rule that build a character set

- Output rule that checks built string

- Generator rule that generates a random string

All types can be customized, ex:

func NewCustomCharset() CharsetRuleFunc {
  return CharsetRuleFunc(func(charset string) string {
    return "abc"
  })
}

Usage:

gen := randomstring.New(randomstring.NewLength(20), NewCustomCharset(), randomstring.NewNoDuplicateCharacters())

Index

Package Files

chars.go charset_rule.go doc.go generate.go generate_rule.go generator.go length_rule.go output_rule.go

Constants

const (
    // Set of lowercase latin letters
    LowerLetters = "abcdefghijklmnopqrstuvwxyz"

    // Set of uppercase latin letters
    UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    // Set of digits
    Digits = "1234567890"

    // Set of symbols
    Symbols = "!\";#$%&'()*+,-./:;<=>?@[]^_`{|}~"
)

Character sets allowed in a generated string

const (
    // Set of letters, digits and symbols which are looks similar
    Similar = "il1Lo0O"

    // Set of symbols which are ambigous
    Ambigous = "{}[]()/\\'\"`~,;:.<>"
)

Character sets disallowed in generated string

func Generate Uses

func Generate(n uint, baseChars ...string) (*string, error)

Generate returns a random string of length n consisting of lower letters, upper letters and digitis

Usage:

randomstring.Generate(20, randomstring.Digits, "@#$^&")

Example output:

1*&$^6^$#*15&$3427$2

type CharsetRuleFunc Uses

type CharsetRuleFunc func(charset string) string

CharsetRuleFunc modify a charset and returns it

func NewExcludeCharset Uses

func NewExcludeCharset(chars string) CharsetRuleFunc

NewExcludeCharset returns a new charset rule func which removes chars from charset

func NewIncludeCharset Uses

func NewIncludeCharset(chars string) CharsetRuleFunc

NewIncludeCharset returns a new charset rule func which add chars to charset

type GenerateRuleFunc Uses

type GenerateRuleFunc func(charset string, length uint, outputRules []OutputRuleFunc) *string

GenerateRuleFunc generates a new string based on: charset, length and output rules

func NewDefaultGenerate Uses

func NewDefaultGenerate() GenerateRuleFunc

NewDefaultGenerate returns a new generate rule func with default random string generator

type Generator Uses

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

Generator is a advanced random string generator based on rules

func New Uses

func New(rules ...interface{}) (*Generator, error)

New creates a new Generator generator

func (*Generator) Generate Uses

func (g *Generator) Generate() (*string, error)

Generate generates a new random string based of rules

type LengthRuleFunc Uses

type LengthRuleFunc func() uint

LengthRuleFunc returns a length of a string to generate

func NewLength Uses

func NewLength(n uint) LengthRuleFunc

NewLength returns a new length rule func which sets string length to n

func NewLengthRange Uses

func NewLengthRange(min, max uint) LengthRuleFunc

NewLengthRange returns a new length rule func which sets string length to length between min and max

type OutputRuleFunc Uses

type OutputRuleFunc func(str []byte, c byte) bool

OutputRuleFunc checks if the string meets the rule

func NewBeginWith Uses

func NewBeginWith(letters string) OutputRuleFunc

NewBeginWith returns a new output rule func that checks if str does start with a one of letters

func NewNoDuplicateCharacters Uses

func NewNoDuplicateCharacters() OutputRuleFunc

NewNoDuplicateCharacters returns a new output rule func that checks if str doesn't have c

func NewNoSequentialCharacters Uses

func NewNoSequentialCharacters(n uint) OutputRuleFunc

NewNoSequentialCharacters returns new output rule func that checks if str doesn't have n sequentials characters

Package randomstring imports 7 packages (graph). Updated 2018-09-10. Refresh now. Tools for package owners.