Skip to content

Commit

Permalink
Merge pull request #14 from wneessen/dev
Browse files Browse the repository at this point in the history
v0.3.1: New password length behaviour
  • Loading branch information
wneessen authored Apr 17, 2021
2 parents 61f37d1 + 31a15d2 commit 3e00103
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 45 deletions.
55 changes: 28 additions & 27 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <length>```: The minimum length of the password to be generated (Default: 20)
- ```-m <length>```: The minimum length of the password to be generated (Default: 12)
- ```-x <length>```: The maximum length of the password to be generated (Default: 20)
- ```-n <number of passwords>```: The amount of passwords to be generated (Default: 1)
- ```-n <number of passwords>```: The amount of passwords to be generated (Default: 6)
- ```-E <list of characters>```: 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)
Expand Down
11 changes: 6 additions & 5 deletions apg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,9 +38,9 @@ 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: 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)
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 41 additions & 1 deletion apg_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "testing"
import (
"testing"
)

var config Config

Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 14 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/wneessen/apg.go
module github.com/wneessen/apg-go

go 1.16

0 comments on commit 3e00103

Please sign in to comment.