2021-03-18 23:26:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-28 11:51:26 +02:00
|
|
|
"flag"
|
2021-03-18 23:26:41 +01:00
|
|
|
"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
|
2021-04-17 11:11:54 +02:00
|
|
|
const DefaultMinLenght int = 12
|
|
|
|
const DefaultMaxLenght int = 20
|
|
|
|
const VersionString string = "0.3.1"
|
2021-03-27 17:03:19 +01:00
|
|
|
|
|
|
|
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-28 11:51:26 +02:00
|
|
|
// Help text
|
2021-04-01 10:53:51 +02:00
|
|
|
const usage = `apg-go // A "Automated Password Generator"-clone
|
|
|
|
Copyright (c) 2021 Winni Neessen
|
|
|
|
|
|
|
|
apg [-m <length>] [-x <length>] [-L] [-U] [-N] [-S] [-H] [-C]
|
|
|
|
[-l] [-M mode] [-E char_string] [-n num_of_pass] [-v] [-h]
|
2021-03-28 11:51:26 +02:00
|
|
|
|
|
|
|
Options:
|
2021-04-17 11:11:54 +02:00
|
|
|
-m LENGTH Minimum length of the password to be generated (Default: 12)
|
2021-03-28 11:51:26 +02:00
|
|
|
-x LENGTH Maximum length of the password to be generated (Default: 20)
|
2021-04-17 11:11:54 +02:00
|
|
|
-n NUMBER Amount of password to be generated (Default: 6)
|
2021-03-28 11:51:26 +02:00
|
|
|
-E CHARS List of characters to be excluded in the generated password
|
|
|
|
-M [LUNSHClunshc] New style password parameters (upper case: on, lower case: off)
|
|
|
|
-L Use lower case characters in passwords (Default: on)
|
|
|
|
-U Use upper case characters in passwords (Default: on)
|
|
|
|
-N Use numeric characters in passwords (Default: on)
|
|
|
|
-S Use special characters in passwords (Default: on)
|
|
|
|
-H Avoid ambiguous characters in passwords (i. e.: 1, l, I, O, 0) (Default: off)
|
|
|
|
-C Enable complex password mode (implies -L -U -N -S and disables -H) (Default: off)
|
|
|
|
-l Spell generated passwords in phonetic alphabet (Default: off)
|
|
|
|
-h Show this help text
|
|
|
|
-v Show version string`
|
|
|
|
|
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
|
2021-03-28 11:51:26 +02:00
|
|
|
flag.Usage = func() { _, _ = fmt.Fprintf(os.Stderr, "%s\n", usage) }
|
2021-03-27 17:03:19 +01:00
|
|
|
var config = parseFlags()
|
|
|
|
|
|
|
|
// Show version and exit
|
|
|
|
if config.showVersion {
|
2021-04-01 10:53:51 +02:00
|
|
|
_, _ = os.Stderr.WriteString(`apg-go // A "Automated Password Generator"-clone v` + VersionString + "\n")
|
2021-03-27 17:03:19 +01:00
|
|
|
_, _ = os.Stderr.WriteString("(C) 2021 by Winni Neessen\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set PW length and available characterset
|
|
|
|
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-04-17 11:11:54 +02:00
|
|
|
pwLength := getPwLengthFromParams(&config)
|
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
|
|
|
}
|
|
|
|
}
|