...

Package password

import "github.com/sethvargo/go-password/password"
Overview
Index
Examples

Overview ▾

Package password provides a library for generating high-entropy random password strings via the crypto/rand package.

res, err := Generate(64, 10, 10, false, false)
if err != nil  {
  log.Fatal(err)
}
log.Printf(res)

Most functions are safe for concurrent use.

Constants

const (
    // LowerLetters is the list of lowercase letters.
    LowerLetters = "abcdefghijklmnopqrstuvwxyz"

    // UpperLetters is the list of uppercase letters.
    UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    // Digits is the list of permitted digits.
    Digits = "0123456789"

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

Variables

var (
    // ErrExceedsTotalLength is the error returned with the number of digits and
    // symbols is greater than the total length.
    ErrExceedsTotalLength = errors.New("number of digits and symbols must be less than total length")

    // ErrLettersExceedsAvailable is the error returned with the number of letters
    // exceeds the number of available letters and repeats are not allowed.
    ErrLettersExceedsAvailable = errors.New("number of letters exceeds available letters and repeats are not allowed")

    // ErrDigitsExceedsAvailable is the error returned with the number of digits
    // exceeds the number of available digits and repeats are not allowed.
    ErrDigitsExceedsAvailable = errors.New("number of digits exceeds available digits and repeats are not allowed")

    // ErrSymbolsExceedsAvailable is the error returned with the number of symbols
    // exceeds the number of available symbols and repeats are not allowed.
    ErrSymbolsExceedsAvailable = errors.New("number of symbols exceeds available symbols and repeats are not allowed")
)

func Generate

func Generate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) (string, error)

Generate is the package shortcut for Generator.Generate.

Example

Code:

res, err := password.Generate(64, 10, 10, false, false)
if err != nil {
    log.Fatal(err)
}
log.Print(res)

func MustGenerate

func MustGenerate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) string

MustGenerate is the package shortcut for Generator.MustGenerate.

Example

Code:

// Will panic on error
res := password.MustGenerate(64, 10, 10, false, false)
log.Print(res)

func NewMockGenerator

func NewMockGenerator(result string, err error) *mockGenerator

NewMockGenerator creates a new generator that satisfies the PasswordGenerator interface. If an error is provided, the error is returned. If a result if provided, the result is always returned, regardless of what parameters are passed into the Generate or MustGenerate methods.

This function is most useful for tests where you want to have predicable results for a transitive resource that depends on go-password.

Example (Testing)

Code:

// Accept a password.PasswordGenerator interface instead of a
// password.Generator struct.
f := func(g password.PasswordGenerator) string {
    // These values don't matter
    return g.MustGenerate(1, 2, 3, false, false)
}

// In tests
gen := password.NewMockGenerator("canned-response", nil)

fmt.Print(f(gen))

Output:

canned-response

type Generator

Generator is the stateful generator which can be used to customize the list of letters, digits, and/or symbols.

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

func NewGenerator

func NewGenerator(i *GeneratorInput) (*Generator, error)

NewGenerator creates a new Generator from the specified configuration. If no input is given, all the default values are used. This function is safe for concurrent use.

Example (Custom)

Code:

// Customize the list of symbols.
gen, err := password.NewGenerator(&password.GeneratorInput{
    Symbols: "!@#$%^()",
})
if err != nil {
    log.Fatal(err)
}

_ = gen // gen.Generate(...)

Example (Nil)

Code:

// This is exactly the same as calling "Generate" directly. It will use all
// the default values.
gen, err := password.NewGenerator(nil)
if err != nil {
    log.Fatal(err)
}

_ = gen // gen.Generate(...)

func (*Generator) Generate

func (g *Generator) Generate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) (string, error)

Generate generates a password with the given requirements. length is the total number of characters in the password. numDigits is the number of digits to include in the result. numSymbols is the number of symbols to include in the result. noUpper excludes uppercase letters from the results. allowRepeat allows characters to repeat.

The algorithm is fast, but it's not designed to be performant; it favors entropy over speed. This function is safe for concurrent use.

Example

Code:

gen, err := password.NewGenerator(nil)
if err != nil {
    log.Fatal(err)
}

res, err := gen.Generate(64, 10, 10, false, false)
if err != nil {
    log.Fatal(err)
}
log.Print(res)

func (*Generator) MustGenerate

func (g *Generator) MustGenerate(length, numDigits, numSymbols int, noUpper, allowRepeat bool) string

MustGenerate is the same as Generate, but panics on error.

type GeneratorInput

GeneratorInput is used as input to the NewGenerator function.

type GeneratorInput struct {
    LowerLetters string
    UpperLetters string
    Digits       string
    Symbols      string
    Reader       io.Reader // rand.Reader by default
}

type PasswordGenerator

PasswordGenerator is an interface that implements the Generate function. This is useful for testing where you can pass this interface instead of a real password generator to mock responses for predicability.

type PasswordGenerator interface {
    Generate(int, int, int, bool, bool) (string, error)
    MustGenerate(int, int, int, bool, bool) string
}