Compare commits

...

2 Commits

Author SHA1 Message Date
brent s. cb35a91855
Adding MustGenerate, GenerateOnce, and MustGenerateOnce.
These are largely just convenience functions/wrappers.
2022-07-07 06:49:18 -04:00
brent s. b12131b661
Adding additional struct tags.
Added toml, yaml, and xml. This will allow for specifying generation schemes in e.g. configuration files for other software.
2022-07-02 03:58:52 -04:00
2 changed files with 58 additions and 15 deletions

View File

@ -46,6 +46,49 @@ func (o *GenOpts) Generate() (passwords []string, err error) {
return
}

// MustGenerate is like Generate, but will panic on error instead of returning.
func (o *GenOpts) MustGenerate() (passwords []string) {

var err error

if passwords, err = o.Generate(); err != nil {
panic(err)
}

return
}

/*
GenOnce generates a *single* password from a GenOpts.

This method is particularly useful/convenient if GenOpts.Count is 1
and you don't want to have to assign to a slice and then get the first index.
*/
func (o GenOpts) GenerateOnce() (password string, err error) {

var passwds []string

o.Count = 1
if passwds, err = o.Generate(); err != nil {
return
}
password = passwds[0]

return
}

// MustGenerateOnce is like GenerateOnce, but will panic on error instead of returning.
func (o *GenOpts) MustGenerateOnce() (password string) {

var err error

if password, err = o.GenerateOnce(); err != nil {
panic(err)
}

return
}

/*
GenerateCollection returns a PwCollection instead of a slice of password text.


View File

@ -30,36 +30,36 @@ type GenOpts struct {

TODO: Get feedback on this. I feel like I'm trimming out WAY too many chars.
*/
// HumanOnly bool `json:"do_human_readable"`
// HumanOnly bool `json:"do_human_readable" toml:"do_human_readable" yaml:"Human-Readable Only" xml:"doHumanReadable,attr"`
// Alpha is true if letters (0x41 to 0x5a, 0x61 to 0x7a) should be included.
Alpha bool `json:"do_alpha"`
Alpha bool `json:"do_alpha" toml:"do_alpha" yaml:"Letters" xml:"doAlpha,attr"`
// Numeric is true if numbers (0x30 to 0x39) should be included.
Numeric bool `json:"do_numeric"`
Numeric bool `json:"do_numeric" toml:"do_numeric" yaml:"Numbers" xml:"doNumeric,attr"`
// Symbols is true if non-alphanumeric characters (between 0x21 and 0x7e) should be included.
Symbols bool `json:"do_symbols"`
Symbols bool `json:"do_symbols" toml:"do_symbols" yaml:"Symbols" xml:"doSymbols,attr"`
// ExtendedSymbols is true if non-alphanumeric characters in the "extended ASCII" set (0x80 to 0xff) should be included.
ExtendedSymbols bool `json:"do_extended"`
ExtendedSymbols bool `json:"do_extended" toml:"do_extended" yaml:"Extended Symbols" xml:"doExtendedSymbols,attr"`
// CountUpper specifies how many uppercase letters (0x41 to 0x5a) should be specified at a minimum.
CountUpper uint `json:"uppers"`
CountUpper uint `json:"uppers" toml:"uppers" yaml:"Number of Uppercase Letters" xml:"numUppers,attr"`
// CountLower specifies how many lowercase letters (0x61 to 0x7a) should be specified at a minimum.
CountLower uint `json:"lowers"`
CountLower uint `json:"lowers" toml:"lowers" yaml:"Number of Lowercase Letters" xml:"numLowers,attr"`
// CountNumbers specifies how many numbers (0x30 to 0x39) should be specified at a minimum.
CountNumbers uint `json:"numbers"`
CountNumbers uint `json:"numbers" toml:"numbers" yaml:"Number of Numeric Characters" xml:"numNumeric,attr"`
// CountSymbols specifies how many symbols (0x21 to 0x7e) should be specified at a minimum.
CountSymbols uint `json:"symbols"`
CountSymbols uint `json:"symbols" toml:"symbols" yaml:"Number of Symbols" xml:"numSymbols,attr"`
// CountExtended specifies how many extended symbols (0x80 to 0xff) should be specified at a minimum.
CountExtended uint `json:"extended"`
CountExtended uint `json:"extended" toml:"extended" yaml:"Number of Extended Symbols" xml:"numXSymbols,attr"`
// DisabledChars includes characters that should NOT be included from the above selection options.
DisabledChars CharSet `json:"disabled_chars"`
DisabledChars CharSet `json:"disabled_chars" toml:"disabled_chars" yaml:"Disabled CharSet" xml:"disabledChars"`
// LengthMin specifies how long (in characters/bytes) each password should be at minimum. Use 0 for no minimum.
LengthMin uint `json:"length_min"`
LengthMin uint `json:"length_min" toml:"length_min" yaml:"Minimum Length" xml:"minLen,attr"`
/*
LengthMax specifies the maximum length for each password. Set to 0 for no limit
(the language has a hard limit of 18446744073709551615; this is limited to 256 for performance reasons).
*/
LengthMax uint `json:"length_max"`
LengthMax uint `json:"length_max" toml:"length_max" yaml:"Maximum Length" xml:"maxLen,attr"`
// Count specifies how many passwords to generate. If 0, the default is 1.
Count uint `json:"count"`
Count uint `json:"count" toml:"count" yaml:"Number of Passwords" xml:"genCount,attr"`
// explicitCharset is the collection of acceptable characters as explicitly defined by the caller, if any.
explicitCharset CharSet
}
@ -79,7 +79,7 @@ type PwDef struct {
// Hashes []PwHashDef `json:"hashes" yaml:"Hashes" xml:"hashes"`
}

// PwHashDef defines a hash for a PwDef (once we implement it).
// PwHashDef defines a hash for a PwDef (once we implement it). Might want to move it to the pwhash subpackage.
type PwHashDef struct {
XMLName xml.Name `json:"-" yaml:"-"`
HashType string `json:"hash_algo" yaml:"Hash Algorithm" xml:"hashAlgo,attr"`