password

package
v0.0.0-...-9392aba Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package password allows generating random passwords

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCommonPassword

func CheckCommonPassword(check func(candidate string) (bool, error), sources ...PasswordSource) error

CheckCommonPassword checks if a password is a common password.

check is called with each candidate password to perform the check. check should return a boolean indicating if the password in question corresponds to the candidate.

CheckCommonPassword returns one of three error values.

- a CommonPasswordError (when a password matches a common password) - an error returned by check (assuming some check went wrong) - or nil (when a password is not a common password

func Generate

func Generate(rand io.Reader, length int, charset Charset) (string, error)

Generate returns a randomly generated string with the provided length and with runes from the given charset.

When an error occurs, it is guaranteed to return "", err. rand is used as a source for randomness

func Passwords

func Passwords(sources ...PasswordSource) <-chan CommonPassword

Passwords returns a channel that contains all passwords in the provided sources. Passwords may be returned in any order.

The caller must drain the channel.

Example
// load all the passwords from common sources
counts := map[string]int{}
for password := range Passwords(CommonSources()...) {

	// do something with the password
	_ = password.Password // string

	// and in this case count it by source
	counts[password.Source]++
}

// output the overall counts!
fmt.Println(counts)
Output:

map[common/top10_000.txt:10000]

Types

type Charset

type Charset string

Charset represents a set of runes to include in a password. An empty Charset is equivalent to DefaultCharSet.

const DefaultCharSet Charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

DefaultCharset represents the default Charset to use.

func (Charset) Contains

func (c Charset) Contains(r rune) bool

func (Charset) ContainsOnly

func (c Charset) ContainsOnly(password string) bool

ContainsOnly checks if password contains only runes from this Charset.

type CommonPassword

type CommonPassword struct {
	Password string
	Source   string
}

type CommonPasswordError

type CommonPasswordError struct {
	CommonPassword
}

CommonPasswordError

func (CommonPasswordError) Error

func (cpe CommonPasswordError) Error() string

type PasswordSource

type PasswordSource interface {
	// Name returns the name of this source
	Name() string

	// Passwords returns a channel that reads all passwords.
	// If an error occurs, returns an empty channel
	//
	// The caller must drain the channel for it to be garbage collected.
	Passwords() <-chan string
}

PasswordSource represents a source of passwords.

func CommonSources

func CommonSources() []PasswordSource

CommonSources returns a list of common password sources

func NewPasswordSource

func NewPasswordSource(open func() (io.Reader, error), name string) PasswordSource

NewPasswordSource creates a new password source from a function

func NewSources

func NewSources(fsys fs.FS, pattern string) ([]PasswordSource, error)

NewSources reads a set of sources from a file system. All files matching pattern are returned.

Jump to

Keyboard shortcuts

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