2022-03-03 04:26:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-22 04:43:12 -04:00
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
2022-03-03 04:26:44 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2022-03-03 05:51:22 -05:00
|
|
|
"strings"
|
2022-03-03 04:26:44 -05:00
|
|
|
|
|
|
|
"github.com/jessevdk/go-flags"
|
2022-05-22 04:43:12 -04:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-03-03 04:26:44 -05:00
|
|
|
"r00t2.io/pwgen/pwgenerator"
|
|
|
|
)
|
|
|
|
|
|
|
|
var a Arguments
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var genOpts *pwgenerator.GenOpts
|
2022-03-03 05:51:22 -05:00
|
|
|
var passwds []string
|
2022-05-22 04:43:12 -04:00
|
|
|
var pwc *pwgenerator.PwCollection
|
|
|
|
var b []byte
|
2022-03-03 04:26:44 -05:00
|
|
|
|
|
|
|
if _, err = flags.Parse(&a); err != nil {
|
|
|
|
switch flagsErr := err.(type) {
|
|
|
|
case *flags.Error:
|
|
|
|
if flagsErr.Type == flags.ErrHelp {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
log.Panicln(err)
|
|
|
|
default:
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 04:43:12 -04:00
|
|
|
if a.ExplicitLen != 0 {
|
|
|
|
a.MinLen = a.ExplicitLen
|
|
|
|
a.MaxLen = a.ExplicitLen
|
|
|
|
}
|
2022-03-03 04:26:44 -05:00
|
|
|
genOpts = &pwgenerator.GenOpts{
|
|
|
|
Alpha: !a.NoAlpha,
|
|
|
|
Numeric: !a.NoNum,
|
|
|
|
Symbols: !a.NoSymbols,
|
|
|
|
ExtendedSymbols: a.ExtendSymbols,
|
|
|
|
CountUpper: a.NumUpper,
|
|
|
|
CountLower: a.NumLower,
|
2022-03-04 01:04:14 -05:00
|
|
|
CountNumbers: a.NumNumbers,
|
2022-03-03 04:26:44 -05:00
|
|
|
CountSymbols: a.NumSymbols,
|
|
|
|
CountExtended: a.NumExtended,
|
|
|
|
DisabledChars: nil,
|
|
|
|
LengthMin: a.MinLen,
|
|
|
|
LengthMax: a.MaxLen,
|
|
|
|
Count: a.Count,
|
|
|
|
}
|
2022-03-03 05:51:22 -05:00
|
|
|
if a.DisableChars != nil && len(a.DisableChars) != 0 {
|
|
|
|
if genOpts.DisabledChars, err = pwgenerator.GetCharset(a.DisableChars); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.ExplicitChars != nil && len(a.ExplicitChars) != 0 {
|
|
|
|
if err = genOpts.SetExplicitCharset(a.ExplicitChars, false); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 04:43:12 -04:00
|
|
|
if strings.TrimSpace(a.Format) != "" {
|
|
|
|
if pwc, err = genOpts.GenerateCollection(nil); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
switch a.Format {
|
|
|
|
case "json":
|
|
|
|
if b, err = json.MarshalIndent(pwc, "", " "); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(b))
|
|
|
|
case "yaml":
|
|
|
|
if b, err = yaml.Marshal(pwc); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(b))
|
|
|
|
case "xml":
|
|
|
|
if b, err = xml.MarshalIndent(pwc, "", " "); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(b))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if passwds, err = genOpts.Generate(); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
fmt.Println(strings.Join(passwds, "\n"))
|
2022-03-03 05:51:22 -05:00
|
|
|
}
|
2022-03-03 04:26:44 -05:00
|
|
|
}
|