View on GitHub

Password Toolkit for Go

package randomstring

import "github.com/go-passwd/randomstring"
Installation Overview API

Installation

go get -u github.com/go-passwd/randomstring

Overview

Generating a random 20-character string with lower letters, upper letters and digits:

s := randomstring.Generate(20)

Generating a random 20-character string with lower letters:

s := randomstring.Generate(20, randomstring.LowerLetters)

API

Constants

// Character sets allowed in a generated string
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 disallowed in generated string
const (
	// Set of letters, digits and symbols which are looks similar
	Similar = "il1Lo0O"

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

func Generate

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

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

type CharsetRuleFunc

CharsetRuleFunc modify a charset and returns it

type CharsetRuleFunc func(charset string) string

func NewExcludeCharset

func NewExcludeCharset(chars string) CharsetRuleFunc

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

func NewIncludeCharset

func NewIncludeCharset(chars string) CharsetRuleFunc

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

type GenerateRuleFunc

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

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

func NewDefaultGenerate

func NewDefaultGenerate() GenerateRuleFunc

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

type Generator

Generator is a advanced random string generator based on rules

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

func New

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

New creates a new Generator generator

func (*Generator) Generate

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

Generate generates a new random string based of rules

type LengthRuleFunc

LengthRuleFunc returns a length of a string to generate

type LengthRuleFunc func() uint

func NewLength

func NewLength(n uint) LengthRuleFunc

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

func NewLengthRange

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

OutputRuleFunc checks if the string meets the rule

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

func NewBeginWith

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

func NewNoDuplicateCharacters() OutputRuleFunc

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

func NewNoSequentialCharacters

func NewNoSequentialCharacters(n uint) OutputRuleFunc

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