Documentation ¶
Overview ¶
Package password allows generating random passwords
Index ¶
- func CheckCommonPassword(check func(candidate string) (bool, error), sources ...PasswordSource) error
- func Generate(rand io.Reader, length int, charset Charset) (string, error)
- func Passwords(sources ...PasswordSource) <-chan CommonPassword
- type Charset
- type CommonPassword
- type CommonPasswordError
- type PasswordSource
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 ¶
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) ContainsOnly ¶
ContainsOnly checks if password contains only runes from this Charset.
type CommonPassword ¶
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.