genpasswd

package module
v0.0.0-...-95e1da5 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2019 License: MIT Imports: 4 Imported by: 0

README

genpasswd.go

Build Status

This is a simple password generator written in golang. This tool is based and inspired from https://github.com/sethvargo/go-password , thanks Vargo.

Some improvements

  • A simple command interface for users
  • Clipboard support for Linux(Xorg/Wayland), macOS, Windows etc.

Build and Usage

Just run make build to build genpasswd(require go version > 1.11). The genpasswd has many params, including:

  -allow-repeat
    	Allows characters to repeat (default true)
  -copies int
    	Generate copies of password (default 1)
  -digits int
    	Number of digits to include in the password (default 4)
  -length intv
    	Specify the password length (default 16)
  -no-clipboard
    	Do not copy to clipboard
  -no-upper
    	Excludes uppercase letters from the results (default true)
  -symbols int
    	Number of digits to include in the password (default 4)
  -version
    	Print version and exit
Running in Docker

Run make docker_image to build Docker image for gopasswd, if u wanna running this tool in contrainer.

Then, type docker run --rm genpasswd:0.0.1 to run genpasswd after build is completed.

That's it, have fun!

--

README.md from original go-password library.

GoDoc

This library implements generation of random passwords with provided requirements as described by AgileBits 1Password in pure Golang. The algorithm is commonly used when generating website passwords.

The library uses crypto/rand for added randomness.

Sample example passwords this library may generate:

0N[k9PhDqmmfaO`p_XHjVv`HTq|zsH4XiH8umjg9JAGJ#\Qm6lZ,28XF4{X?3sHj
7@90|0H7!4p\,c<!32:)0.9N
UlYuRtgqyWEivlXnLeBpZvIQ
Q795Im1VR5h363s48oZGaLDa
wpvbxlsc

Installation

$ go get -u github.com/sethvargo/go-password/password

Usage

package main

import (
  "log"

  "github.com/sethvargo/go-password/password"
)

func main() {
  // Generate a password that is 64 characters long with 10 digits, 10 symbols,
  // allowing upper and lower case letters, disallowing repeat characters.
  res, err := password.Generate(64, 10, 10, false, false)
  if err != nil {
    log.Fatal(err)
  }
  log.Printf(res)
}

See the GoDoc for more information.

License

This code is licensed under the MIT license.

Documentation

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.

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 = "~!@#$%^&*()_+`-={}|[]\\:\"<>?,./"
)

Variables

View Source
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")
)

Functions

func Generate

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

Generate is the package shortcut for Generator.Generate.

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

func MustGenerate

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

MustGenerate is the package shortcut for Generator.MustGenerate.

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

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(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)
// Customize the list of symbols.
gen, err := NewGenerator(&GeneratorInput{
	Symbols: "!@#$%^()",
})
if err != nil {
	log.Fatal(err)
}

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

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

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

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
gen, err := 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)
Output:

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

type GeneratorInput struct {
	LowerLetters string
	UpperLetters string
	Digits       string
	Symbols      string
}

GeneratorInput is used as input to the NewGenerator function.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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