Help us shape the future of Go! Take the 2026 Go Developer Survey

passwordgen

package module
v0.0.0-...-f5265eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2019 License: MIT Imports: 4 Imported by: 0

README

Golang Password Generator

GoDoc

This library provides a password generated backed by crypto/rand that allows for deep custimization of generated password. passwordgen allows you to specify which character groups are in the password gen pool. You can also specify a mininum number of characters from a group that must be in the password, or even specify the exact number of characters from a group.

Installation

$ go get -u github.com/kenXengineering/passwordgen

Usage

Generate a password with lower, upper case characters, digits, and symbols.

package main

import (
    "log"
    "github.com/kenXengineering/passwordgen"
)

func main() {
    pass, err := passwordgen.NewGenerator().WithLower().WithUpper().WithDigits().WithSymbols().Generate(16)
    if err != nil {
        log.Fatal(err)
    }
    log.Print(pass)
}

Generate a password with at least 1 lower case character, 1 upper case character, 2 digits, and no symbols.

package main

import (
    "log"
    "github.com/kenXengineering/passwordgen"
)

func main() {
    pass, err := passwordgen.NewGenerator().RequireLower(1).RequireUpper(1).RequireDigits(2).Generate(8)
    if err != nil {
        log.Fatal(err)
    }
    log.Print(pass)
}

Generate a password with lower case, upper case characters, at least 2 digits, and exactly 2 symbols.

package main

import (
    "log"
    "github.com/kenXengineering/passwordgen"
)

func main() {
    pass, err := passwordgen.NewGenerator().WithLower().WithUpper().RequireDigits(2).ExactSymbol(2).Generate(8)
    if err != nil {
        log.Fatal(err)
    }
    log.Print(pass)
}

Create a generator that can be used later on.

package main

import (
    "log"
    "github.com/kenXengineering/passwordgen"
)

func main() {
    generator := passwordgen.NewGenerator().WithLower().WithUpper().WithDigits().WithSymbols()

    // .............

    pass, err := generator.Generate(16)
    if err != nil {
        log.Fatal(err)
    }
    log.Print(pass)
}

See the GoDoc for more information.

License

This code is licensed under the MIT license.

Documentation

Index

Examples

Constants

View Source
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 = "~!@#$%^&*()_+-={}[]"

	// LowerLettersNoAmbig is the list of lowercase letters.
	LowerLettersNoAmbig = "abcdefghjkmnpqrstuvwxyz"

	// UpperLettersNoAmbig is the list of uppercase letters.
	UpperLettersNoAmbig = "ABCDEFGHJKMNPQRSTUVWXYZ"

	// DigitsNoAmbig is the list of permitted digits.
	DigitsNoAmbig = "23456789"

	// SymbolsNoAmbig is the list of symbols.
	SymbolsNoAmbig = "~!@#$%^&*()_+-={}[]"
)

Variables

View Source
var (
	// ErrExceedsTotalLength is the error returned when the number of required
	// elements is greater then the length of the requestd password
	ErrExceedsTotalLength = errors.New("the number of required elements exceeds requested password length")

	// ErrNoCharactersSpecified is the error returned when a generator is called
	// without any characters specified
	ErrNoCharactersSpecified = errors.New("no characters specified in generator")
)

Functions

This section is empty.

Types

type Generator

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

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

func NewGenerator

func NewGenerator() *Generator

NewGenerator Returns a new empty generator.

func (*Generator) ExactDigits

func (g *Generator) ExactDigits(N int) *Generator

ExactDigits guarantees that there are exactly N digits in the generated password.

func (*Generator) ExactLower

func (g *Generator) ExactLower(N int) *Generator

ExactLower guarantees that there are exactly N lower case letters in the generated password.

func (*Generator) ExactSymbols

func (g *Generator) ExactSymbols(N int) *Generator

ExactSymbols guarantees that there are exactly N symbols in the generated password.

func (*Generator) ExactUpper

func (g *Generator) ExactUpper(N int) *Generator

ExactUpper guarantees that there are exactly N upper case letters in the generated password.

Example
pass, err := NewGenerator().ExactUpper(3).WithLower().Generate(8)
if err != nil {
	log.Fatal(err)
}
log.Print(pass)

func (*Generator) Generate

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

Generate will generate a password at the specified length as configured.

Example
pass, err := NewGenerator().WithUpper().WithLower().WithDigits().WithLower().Generate(8)
if err != nil {
	log.Fatal(err)
}
log.Print(pass)

func (*Generator) NoAmbiguousCharacters

func (g *Generator) NoAmbiguousCharacters() *Generator

NoAmbiguousCharacters ensures no ambiguous characters will be in the password.

Example
pass, err := NewGenerator().NoAmbiguousCharacters().WithUpper().WithLower().WithDigits().WithSymbols().Generate(16)
if err != nil {
	log.Fatal(err)
}
log.Print(pass)

func (*Generator) RequireDigits

func (g *Generator) RequireDigits(N int) *Generator

RequireDigits guarantees that at least N number of digits will be in the generated password.

func (*Generator) RequireLower

func (g *Generator) RequireLower(N int) *Generator

RequireLower guarantees that at least N number of lower case letters will be in the generated password.

func (*Generator) RequireSymbols

func (g *Generator) RequireSymbols(N int) *Generator

RequireSymbols guarantees that at least N number of symbols will be in the generated password.

func (*Generator) RequireUpper

func (g *Generator) RequireUpper(N int) *Generator

RequireUpper guarantees that at least N number of upper case letters will be in the generated password.

Example
pass, err := NewGenerator().RequireUpper(3).WithLower().WithDigits().WithSymbols().Generate(8)

if err != nil {
	log.Fatal(err)
}

log.Print(pass)

func (*Generator) WithDigits

func (g *Generator) WithDigits() *Generator

WithDigits adds digits to the password pool. Does not guarantee digits will be present in the generated password.

func (*Generator) WithLower

func (g *Generator) WithLower() *Generator

WithLower adds lower case letters to the password pool. Does not guarantee lower case letters will be present in the generated password.

func (*Generator) WithSymbols

func (g *Generator) WithSymbols() *Generator

WithSymbols adds symbols to the password pool. Does not guarantee symbols will be present in the generated password.

func (*Generator) WithUpper

func (g *Generator) WithUpper() *Generator

WithUpper adds upper case letters to the password pool. Does not guarantee upper case letters will be present in the generated password.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL