2021-03-18 23:26:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-22 17:53:36 +01:00
|
|
|
"log"
|
2021-03-27 17:03:19 +01:00
|
|
|
"os"
|
2021-03-18 23:26:41 +01:00
|
|
|
)
|
|
|
|
|
2021-03-27 17:03:19 +01:00
|
|
|
// Constants
|
|
|
|
const DefaultPwLenght int = 20
|
|
|
|
const VersionString string = "0.2.8"
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
minPassLen int
|
|
|
|
maxPassLen int
|
|
|
|
numOfPass int
|
|
|
|
useComplex bool
|
|
|
|
useLowerCase bool
|
|
|
|
useUpperCase bool
|
|
|
|
useNumber bool
|
|
|
|
useSpecial bool
|
|
|
|
humanReadable bool
|
|
|
|
excludeChars string
|
|
|
|
newStyleModes string
|
|
|
|
spellPassword bool
|
|
|
|
ShowHelp bool
|
|
|
|
showVersion bool
|
|
|
|
outputMode int
|
|
|
|
}
|
|
|
|
|
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-26 14:27:54 +01:00
|
|
|
// Log config
|
|
|
|
log.SetFlags(log.Ltime | log.Ldate | log.Lshortfile)
|
|
|
|
|
2021-03-27 17:03:19 +01:00
|
|
|
// Read and parse flags
|
|
|
|
var config = parseFlags()
|
|
|
|
|
|
|
|
// Show version and exit
|
|
|
|
if config.showVersion {
|
|
|
|
_, _ = os.Stderr.WriteString("Advanced Password Generator Clone (apg.go) v" + VersionString + "\n")
|
|
|
|
_, _ = os.Stderr.WriteString("(C) 2021 by Winni Neessen\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set PW length and available characterset
|
|
|
|
pwLength := getPwLengthFromParams(&config)
|
|
|
|
charRange := getCharRange(&config)
|
2021-03-21 13:25:52 +01:00
|
|
|
|
2021-03-26 14:27:54 +01:00
|
|
|
// Generate passwords
|
2021-03-27 17:03:19 +01:00
|
|
|
for i := 1; i <= config.numOfPass; i++ {
|
2021-03-21 13:25:52 +01:00
|
|
|
pwString, err := getRandChar(&charRange, pwLength)
|
|
|
|
if err != nil {
|
2021-03-22 17:53:36 +01:00
|
|
|
log.Fatalf("getRandChar returned an error: %q\n", err)
|
2021-03-21 13:25:52 +01:00
|
|
|
}
|
2021-03-21 15:28:23 +01:00
|
|
|
|
2021-03-27 17:03:19 +01:00
|
|
|
switch config.outputMode {
|
2021-03-21 15:28:23 +01:00
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
spelledPw, err := spellPasswordString(pwString)
|
|
|
|
if err != nil {
|
2021-03-22 17:53:36 +01:00
|
|
|
log.Fatalf("spellPasswordString returned an error: %q\n", err.Error())
|
2021-03-21 15:28:23 +01:00
|
|
|
}
|
|
|
|
fmt.Printf("%v (%v)\n", pwString, spelledPw)
|
2021-03-21 19:25:54 +01:00
|
|
|
break
|
2021-03-21 15:28:23 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
fmt.Println(pwString)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 13:25:52 +01:00
|
|
|
}
|
|
|
|
}
|