2021-03-18 23:26:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
const DefaultPwLenght int = 20
|
2021-03-21 14:05:14 +01:00
|
|
|
const VersionString string = "0.2.4"
|
2021-03-18 23:26:41 +01:00
|
|
|
const PwLowerCharsHuman string = "abcdefghjkmnpqrstuvwxyz"
|
|
|
|
const PwUpperCharsHuman string = "ABCDEFGHJKMNPQRSTUVWXYZ"
|
|
|
|
const PwLowerChars string = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
const PwUpperChars string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2021-03-21 13:25:52 +01:00
|
|
|
const PwSpecialCharsHuman string = "\"#%*+-/:;=\\_|~"
|
|
|
|
const PwSpecialChars string = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
2021-03-18 23:26:41 +01:00
|
|
|
const PwNumbersHuman string = "23456789"
|
|
|
|
const PwNumbers string = "1234567890"
|
|
|
|
|
|
|
|
type cliOpts struct {
|
|
|
|
minPassLen int
|
|
|
|
maxPassLen int
|
|
|
|
numOfPass int
|
|
|
|
useComplex bool
|
|
|
|
useLowerCase bool
|
|
|
|
useUpperCase bool
|
|
|
|
useNumber bool
|
|
|
|
useSpecial bool
|
|
|
|
humanReadable bool
|
|
|
|
excludeChars string
|
2021-03-21 14:05:14 +01:00
|
|
|
newStyleModes string
|
2021-03-21 15:28:23 +01:00
|
|
|
spellPassword bool
|
2021-03-18 23:26:41 +01:00
|
|
|
showHelp bool
|
|
|
|
showVersion bool
|
2021-03-21 15:28:23 +01:00
|
|
|
outputMode int
|
2021-03-18 23:26:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var config cliOpts
|
|
|
|
|
|
|
|
// Read flags
|
|
|
|
func init() {
|
|
|
|
// Bool flags
|
|
|
|
flag.BoolVar(&config.useLowerCase, "L", true, "Use lower case characters in passwords")
|
|
|
|
flag.BoolVar(&config.useUpperCase, "U", false, "Use upper case characters in passwords")
|
|
|
|
flag.BoolVar(&config.useNumber, "N", false, "Use numbers in passwords")
|
|
|
|
flag.BoolVar(&config.useSpecial, "S", false, "Use special characters in passwords")
|
2021-03-21 15:28:23 +01:00
|
|
|
flag.BoolVar(&config.useComplex, "C", false, "Generate complex passwords (implies -L -U -N -S, disables -H)")
|
|
|
|
flag.BoolVar(&config.spellPassword, "l", false, "Spell generated password")
|
2021-03-18 23:26:41 +01:00
|
|
|
flag.BoolVar(&config.humanReadable, "H", false, "Generate human-readable passwords")
|
|
|
|
flag.BoolVar(&config.showVersion, "v", false, "Show version")
|
|
|
|
|
|
|
|
// Int flags
|
|
|
|
flag.IntVar(&config.minPassLen, "m", 10, "Minimum password length")
|
|
|
|
flag.IntVar(&config.maxPassLen, "x", DefaultPwLenght, "Maxiumum password length")
|
|
|
|
flag.IntVar(&config.numOfPass, "n", 1, "Number of passwords to generate")
|
|
|
|
|
2021-03-18 23:40:18 +01:00
|
|
|
// String flags
|
|
|
|
flag.StringVar(&config.excludeChars, "E", "", "Exclude list of characters from generated password")
|
2021-03-21 14:05:14 +01:00
|
|
|
flag.StringVar(&config.newStyleModes, "M", "",
|
|
|
|
"New style password parameters (higher priority than single parameters)")
|
2021-03-18 23:26:41 +01:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
if config.showVersion {
|
2021-03-21 13:25:52 +01:00
|
|
|
_, _ = os.Stderr.WriteString("Winni's Advanced Password Generator Clone (apg.go) v" + VersionString + "\n")
|
2021-03-18 23:26:41 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:25:52 +01:00
|
|
|
// Main function that generated the passwords and returns them
|
2021-03-18 23:26:41 +01:00
|
|
|
func main() {
|
2021-03-21 14:05:14 +01:00
|
|
|
parseParams()
|
|
|
|
pwLength := getPwLengthFromParams()
|
2021-03-21 13:25:52 +01:00
|
|
|
charRange := getCharRange()
|
|
|
|
|
|
|
|
for i := 1; i <= config.numOfPass; i++ {
|
|
|
|
pwString, err := getRandChar(&charRange, pwLength)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("getRandChar returned an error: %q\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-03-21 15:28:23 +01:00
|
|
|
|
|
|
|
switch config.outputMode {
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
spelledPw, err := spellPasswordString(pwString)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("spellPasswordString returned an error: %q\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("%v (%v)\n", pwString, spelledPw)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
fmt.Println(pwString)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 13:25:52 +01:00
|
|
|
}
|
|
|
|
}
|