mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-14 01:42:55 +01:00
Winni Neessen
eaf4a4c57d
To address issue #13, the password length behaviour of the original APG has been reproduced. Previously, when a minLength of 5 and a maxLength of 10 was given, apg-go se the pwLength to the preferred maxLength. With v0.3.1 it will choose a random length between minLength and maxLength instead, same as the original C-lang apg did. For this the minLength has been defaulted to a sane value of 12 (instead of the 8 of the original apg). The default for maxLength stayed at 20. Also the default number of generated passwords has been changed from 1 to 6, to replicate the behaviour of the original apg.
100 lines
2.9 KiB
Go
100 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
// Constants
|
|
const DefaultMinLenght int = 12
|
|
const DefaultMaxLenght int = 20
|
|
const VersionString string = "0.3.1"
|
|
|
|
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
|
|
}
|
|
|
|
// Help text
|
|
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]
|
|
|
|
Options:
|
|
-m LENGTH Minimum length of the password to be generated (Default: 12)
|
|
-x LENGTH Maximum length of the password to be generated (Default: 20)
|
|
-n NUMBER Amount of password to be generated (Default: 6)
|
|
-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`
|
|
|
|
// Main function that generated the passwords and returns them
|
|
func main() {
|
|
// Log config
|
|
log.SetFlags(log.Ltime | log.Ldate | log.Lshortfile)
|
|
|
|
// Read and parse flags
|
|
flag.Usage = func() { _, _ = fmt.Fprintf(os.Stderr, "%s\n", usage) }
|
|
var config = parseFlags()
|
|
|
|
// Show version and exit
|
|
if config.showVersion {
|
|
_, _ = os.Stderr.WriteString(`apg-go // A "Automated Password Generator"-clone v` + VersionString + "\n")
|
|
_, _ = os.Stderr.WriteString("(C) 2021 by Winni Neessen\n")
|
|
os.Exit(0)
|
|
}
|
|
|
|
// Set PW length and available characterset
|
|
charRange := getCharRange(&config)
|
|
|
|
// Generate passwords
|
|
for i := 1; i <= config.numOfPass; i++ {
|
|
pwLength := getPwLengthFromParams(&config)
|
|
pwString, err := getRandChar(&charRange, pwLength)
|
|
if err != nil {
|
|
log.Fatalf("getRandChar returned an error: %q\n", err)
|
|
}
|
|
|
|
switch config.outputMode {
|
|
case 1:
|
|
{
|
|
spelledPw, err := spellPasswordString(pwString)
|
|
if err != nil {
|
|
log.Fatalf("spellPasswordString returned an error: %q\n", err.Error())
|
|
}
|
|
fmt.Printf("%v (%v)\n", pwString, spelledPw)
|
|
break
|
|
}
|
|
default:
|
|
{
|
|
fmt.Println(pwString)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|