acopw-go – git.sr.ht/~jamesponddotco/acopw-go Index | Examples | Files

package acopw

import "git.sr.ht/~jamesponddotco/acopw-go"

Package acopw provides a simple way to generate cryptographically secure random and diceware passwords, and PINs.

Index

Examples

Constants

const DefaultDicewareLength int = 8

DefaultDicewareLength is the default length of a diceware password.

const DefaultPINLength int = 6

DefaultPINLength is the default length of a PIN.

const DefaultRandomLength int = 128

DefaultRandomLength is the default length of a random password.

const ErrInvalidCharset xerrors.Error = "no characters to build password in the charset"

ErrInvalidCharset is returned when the internal character set is empty.

Types

type Diceware

type Diceware struct {

	// Separator is the string used to separate words in the password.
	Separator string

	// Words is a list of words included in the generated password. If the list
	// is empty, a default word list is used.
	Words []string

	// Length is the number of words to include in the generated password. If
	// less than 1, it defaults to 8.
	Length int

	// Capitalize indicates whether a random word should be capitalized.
	Capitalize bool
	// contains filtered or unexported fields
}

Diceware is a policy for generating ChaCha8-based cryptographically strong diceware passwords.

func (*Diceware) Generate

func (d *Diceware) Generate() string

Generate returns a cryptographically strong diceware password for the policy. It panics if it can't get entropy from the source of randomness.

Example

Code:

{
	// Define password policy.
	password := acopw.Diceware{
		Length:     7,    // Use 7 words.
		Capitalize: true, // Capitalize the first letter of a random word.
	}

	// Generate and print a random diceware password. Use os.Stdout in the real
	// world.
	if _, err := fmt.Fprintln(os.Stderr, password.Generate()); err != nil {
		log.Fatal(err)
	}
	// Output:
}

type PIN

type PIN struct {

	// Length is the length of the generated PIN. If less than 1, it defaults to
	// 6.
	Length int
	// contains filtered or unexported fields
}

PIN is a policy for generating ChaCha8-based cryptographically strong random PINs.

func (*PIN) Generate

func (p *PIN) Generate() string

Generate returns a cryptographically strong PIN for the policy. It panics if it can't get entropy from the source of randomness.

Example

Code:

{
	// Define your PIN policy.
	pin := acopw.PIN{
		Length: 6, // Generate a 6 digit PIN.
	}

	// Generate and print a random PIN. Use os.Stdout in the real world.
	if _, err := fmt.Fprintln(os.Stderr, pin.Generate()); err != nil {
		log.Fatal(err)
	}
	// Output:
	//
}

type Random

type Random struct {

	// ExcludedCharset is a list of characters that should not be included in
	// the generated password.
	ExcludedCharset []string

	// Length is the length of the password. If less than 1, it defaults to 128.
	Length int

	// UseLower, UseUpper, UseNumbers, and UseSymbols specify whether or not to
	// use the corresponding character class in the generated password.
	//
	// If none of these are true, it defaults to true for all four.
	UseLower   bool
	UseUpper   bool
	UseNumbers bool
	UseSymbols bool
	// contains filtered or unexported fields
}

Random is a policy for generating ChaCha8-based cryptographically strong random passwords.

func (*Random) Generate

func (r *Random) Generate() string

Generate returns a cryptographically strong random password for the policy. It panics if it can't get entropy from the source of randomness or if the internally generated character set is empty.

Example

Code:

{
	// Define password policy.
	password := acopw.Random{
		ExcludedCharset: []string{
			" ", // Exclude spaces
			"&", // Exclude ampersands
		},
		Length:     64,   // Generate a 64 character password
		UseLower:   true, // Use lowercase letters
		UseUpper:   true, // Use uppercase letters
		UseSymbols: true, // Use symbols
	}

	// Generate and print a random password. Use os.Stdout in the real world.
	if _, err := fmt.Fprintln(os.Stderr, password.Generate()); err != nil {
		log.Fatal(err)
	}
	// Output:
	//
}

Source Files

acopw.go diceware.go pin.go random.go

Version
v1.0.2 (latest)
Published
Apr 20, 2024
Platform
linux/amd64
Imports
7 packages
Last checked
1 day ago

Tools for package owners.