diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 42cea83..285df04 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,10 +4,10 @@
-
+
-
-
+
+
@@ -49,7 +49,6 @@
-
@@ -71,15 +70,15 @@
-
+
-
-
+
+
@@ -87,9 +86,9 @@
-
-
+
+
@@ -97,9 +96,9 @@
-
-
+
+
@@ -107,9 +106,9 @@
-
-
+
+
@@ -117,19 +116,19 @@
-
-
+
+
-
+
-
-
+
+
@@ -137,38 +136,38 @@
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -232,7 +231,9 @@
-
+
+
+
true
diff --git a/README.md b/README.md
index 1e9b92f..e61faa0 100644
--- a/README.md
+++ b/README.md
@@ -85,9 +85,9 @@ $ sudo cp apg /usr/local/bin/apg
## CLI parameters
_apg.go_ replicates some of the parameters of the original APG. Some parameters are different though:
-- ```-m ```: The minimum length of the password to be generated (Default: 20)
+- ```-m ```: The minimum length of the password to be generated (Default: 12)
- ```-x ```: The maximum length of the password to be generated (Default: 20)
-- ```-n ```: The amount of passwords to be generated (Default: 1)
+- ```-n ```: The amount of passwords to be generated (Default: 6)
- ```-E ```: Do not use the specified characters in generated passwords
- ```-M <[LUNSHClunshc]>```: New style password parameters (upper-case enables, lower-case disables)
- ```-L```: Use lower-case characters in passwords (Default: on)
diff --git a/apg.go b/apg.go
index b11a5db..4d7c374 100644
--- a/apg.go
+++ b/apg.go
@@ -8,8 +8,9 @@ import (
)
// Constants
-const DefaultPwLenght int = 20
-const VersionString string = "0.3.0"
+const DefaultMinLenght int = 12
+const DefaultMaxLenght int = 20
+const VersionString string = "0.3.1"
type Config struct {
minPassLen int
@@ -37,9 +38,9 @@ apg [-m ] [-x ] [-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: 20)
+ -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: 1)
+ -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)
@@ -69,11 +70,11 @@ func main() {
}
// Set PW length and available characterset
- pwLength := getPwLengthFromParams(&config)
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)
diff --git a/apg_test.go b/apg_test.go
index 5e8183f..8b59a9c 100644
--- a/apg_test.go
+++ b/apg_test.go
@@ -1,6 +1,8 @@
package main
-import "testing"
+import (
+ "testing"
+)
var config Config
@@ -51,6 +53,44 @@ func TestGetRandNum(t *testing.T) {
}
}
+// Test Pwlength
+func TestGenLength(t *testing.T) {
+ testTable := []struct {
+ testName string
+ minLength int
+ maxLength int
+ }{
+ {"pwLength defaults", DefaultMinLenght, DefaultMaxLenght},
+ {"pwLength 0 to 1", 0, 1},
+ {"pwLength 1 to 10", 0, 10},
+ {"pwLength 10 to 100", 10, 100},
+ }
+
+ charRange := getCharRange(&config)
+ for _, testCase := range testTable {
+ t.Run(testCase.testName, func(t *testing.T) {
+ config.minPassLen = testCase.minLength
+ config.maxPassLen = testCase.maxLength
+ pwLength := getPwLengthFromParams(&config)
+ for i := 0; i < 1000; i++ {
+ pwString, err := getRandChar(&charRange, pwLength)
+ if err != nil {
+ t.Errorf("getRandChar returned an error: %q", err)
+ }
+ retLen := len(pwString)
+ if retLen > testCase.maxLength {
+ t.Errorf("Generated password length too long. GivenMin %v, GivenMax: %v, Returned length %v",
+ testCase.minLength, testCase.maxLength, retLen)
+ }
+ if retLen < testCase.minLength {
+ t.Errorf("Generated password length too short. GivenMin %v, GivenMax: %v, Returned length %v",
+ testCase.minLength, testCase.maxLength, retLen)
+ }
+ }
+ })
+ }
+}
+
// Test getRandChar
func TestGetRandChar(t *testing.T) {
t.Run("return_value_is_A_B_or_C", func(t *testing.T) {
diff --git a/config.go b/config.go
index 801a89c..04ef4aa 100644
--- a/config.go
+++ b/config.go
@@ -18,9 +18,9 @@ func parseFlags() Config {
flag.BoolVar(&config.spellPassword, "l", false, "Spell generated password")
flag.BoolVar(&config.humanReadable, "H", false, "Generate human-readable passwords")
flag.BoolVar(&config.showVersion, "v", false, "Show version")
- flag.IntVar(&config.minPassLen, "m", DefaultPwLenght, "Minimum password length")
- flag.IntVar(&config.maxPassLen, "x", DefaultPwLenght, "Maxiumum password length")
- flag.IntVar(&config.numOfPass, "n", 1, "Number of passwords to generate")
+ flag.IntVar(&config.minPassLen, "m", DefaultMinLenght, "Minimum password length")
+ flag.IntVar(&config.maxPassLen, "x", DefaultMaxLenght, "Maxiumum password length")
+ flag.IntVar(&config.numOfPass, "n", 6, "Number of passwords to generate")
flag.StringVar(&config.excludeChars, "E", "", "Exclude list of characters from generated password")
flag.StringVar(&config.newStyleModes, "M", "",
"New style password parameters (higher priority than single parameters)")
@@ -60,15 +60,20 @@ func parseParams(config *Config) {
// Get the password length from the given cli flags
func getPwLengthFromParams(config *Config) int {
- pwLength := config.minPassLen
- if pwLength < config.minPassLen {
- pwLength = config.minPassLen
+ if config.minPassLen > config.maxPassLen {
+ config.maxPassLen = config.minPassLen
}
- if pwLength > config.maxPassLen {
- pwLength = config.maxPassLen
+ lenDiff := config.maxPassLen - config.minPassLen + 1
+ randAdd, err := getRandNum(lenDiff)
+ if err != nil {
+ log.Fatalf("Failed to generated password length: %v", err)
+ }
+ retVal := config.minPassLen + randAdd
+ if retVal <= 0 {
+ return 1
}
- return pwLength
+ return retVal
}
// Parse the new style parameters
diff --git a/go.mod b/go.mod
index dd33c59..b6933e8 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,3 @@
-module github.com/wneessen/apg.go
+module github.com/wneessen/apg-go
go 1.16