v0.3.1: New password length behaviour

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.
This commit is contained in:
Winni Neessen 2021-04-17 11:04:14 +02:00
parent f13ffdf406
commit 9277e83fd4
Signed by: wneessen
GPG key ID: 385AC9889632126E
5 changed files with 81 additions and 38 deletions

View file

@ -6,8 +6,11 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="fbb0c733-4aa1-4d27-87d5-c7276d8aa613" name="Default Changelist" comment=""> <list default="true" id="fbb0c733-4aa1-4d27-87d5-c7276d8aa613" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" /> <change beforePath="$PROJECT_DIR$/apg.go" beforeDir="false" afterPath="$PROJECT_DIR$/apg.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/buildfiles/arch-linux/PKGBUILD" beforeDir="false" afterPath="$PROJECT_DIR$/buildfiles/arch-linux/PKGBUILD" afterDir="false" /> <change beforePath="$PROJECT_DIR$/apg_test.go" beforeDir="false" afterPath="$PROJECT_DIR$/apg_test.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/config.go" beforeDir="false" afterPath="$PROJECT_DIR$/config.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
<change beforePath="$PROJECT_DIR$/rand.go" beforeDir="false" afterPath="$PROJECT_DIR$/rand.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -49,7 +52,6 @@
<component name="ProjectViewState"> <component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
<option name="showMembers" value="true" />
</component> </component>
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="DefaultGoTemplateProperty" value="Go Application" /> <property name="DefaultGoTemplateProperty" value="Go Application" />
@ -71,15 +73,15 @@
<recent name="C:\Users\Winni Neessen\go\src\apg.go\test" /> <recent name="C:\Users\Winni Neessen\go\src\apg.go\test" />
</key> </key>
</component> </component>
<component name="RunManager" selected="Go Test.Test Application"> <component name="RunManager" selected="Go Build.Run Application (with newstyle params)">
<configuration name="Run Application (show help text)" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application (show help text)" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="apg.go" /> <module name="apg.go" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<parameters value="-h" /> <parameters value="-h" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Run Application (show version string)" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application (show version string)" type="GoApplicationRunConfiguration" factoryName="Go Application">
@ -87,9 +89,9 @@
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<parameters value="-v" /> <parameters value="-v" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Run Application (with excludes)" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application (with excludes)" type="GoApplicationRunConfiguration" factoryName="Go Application">
@ -97,9 +99,9 @@
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<parameters value="-m 20 -x 20 -C -E abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!]" /> <parameters value="-m 20 -x 20 -C -E abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!]" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Run Application (with newstyle params and spelling and multiple pws)" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application (with newstyle params and spelling and multiple pws)" type="GoApplicationRunConfiguration" factoryName="Go Application">
@ -107,9 +109,9 @@
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<parameters value="-m 10 -x 10 -M SLNU -l -n 6" /> <parameters value="-m 10 -x 10 -M SLNU -l -n 6" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Run Application (with newstyle params and spelling)" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application (with newstyle params and spelling)" type="GoApplicationRunConfiguration" factoryName="Go Application">
@ -117,19 +119,19 @@
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<parameters value="-m 10 -x 10 -M SLNU -l" /> <parameters value="-m 10 -x 10 -M SLNU -l" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Run Application (with newstyle params)" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application (with newstyle params)" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="apg.go" /> <module name="apg.go" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<parameters value="-m 20 -x 20 -M slNU" /> <parameters value="-M slNU" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Run Application (with params)" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application (with params)" type="GoApplicationRunConfiguration" factoryName="Go Application">
@ -137,38 +139,38 @@
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<parameters value="-m 20 -x 20 -C" /> <parameters value="-m 20 -x 20 -C" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Run Application" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="Run Application" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="apg.go" /> <module name="apg.go" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$" /> <package value="github.com/wneessen/apg-go" />
<package value="github.com/wneessen/apg.go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Benchmark Application" type="GoTestRunConfiguration" factoryName="Go Test"> <configuration name="Benchmark Application" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="apg.go" /> <module name="apg.go" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<framework value="gobench" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<package value="github.com/wneessen/apg.go" /> <package value="github.com/wneessen/apg-go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" /> <filePath value="$PROJECT_DIR$" />
<framework value="gobench" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="Test Application" type="GoTestRunConfiguration" factoryName="Go Test"> <configuration name="Test Application" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="apg.go" /> <module name="apg.go" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
<framework value="gotest" />
<kind value="PACKAGE" /> <kind value="PACKAGE" />
<package value="github.com/wneessen/apg.go" /> <package value="github.com/wneessen/apg-go" />
<directory value="$PROJECT_DIR$" /> <directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" /> <filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<list> <list>

View file

@ -85,9 +85,9 @@ $ sudo cp apg /usr/local/bin/apg
## CLI parameters ## CLI parameters
_apg.go_ replicates some of the parameters of the original APG. Some parameters are different though: _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) - ```-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 - ```-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) - ```-M <[LUNSHClunshc]>```: New style password parameters (upper-case enables, lower-case disables)
- ```-L```: Use lower-case characters in passwords (Default: on) - ```-L```: Use lower-case characters in passwords (Default: on)

View file

@ -1,6 +1,8 @@
package main package main
import "testing" import (
"testing"
)
var config Config 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 // Test getRandChar
func TestGetRandChar(t *testing.T) { func TestGetRandChar(t *testing.T) {
t.Run("return_value_is_A_B_or_C", func(t *testing.T) { t.Run("return_value_is_A_B_or_C", func(t *testing.T) {

View file

@ -18,9 +18,9 @@ func parseFlags() Config {
flag.BoolVar(&config.spellPassword, "l", false, "Spell generated password") flag.BoolVar(&config.spellPassword, "l", false, "Spell generated password")
flag.BoolVar(&config.humanReadable, "H", false, "Generate human-readable passwords") flag.BoolVar(&config.humanReadable, "H", false, "Generate human-readable passwords")
flag.BoolVar(&config.showVersion, "v", false, "Show version") flag.BoolVar(&config.showVersion, "v", false, "Show version")
flag.IntVar(&config.minPassLen, "m", DefaultPwLenght, "Minimum password length") flag.IntVar(&config.minPassLen, "m", DefaultMinLenght, "Minimum password length")
flag.IntVar(&config.maxPassLen, "x", DefaultPwLenght, "Maxiumum password length") flag.IntVar(&config.maxPassLen, "x", DefaultMaxLenght, "Maxiumum password length")
flag.IntVar(&config.numOfPass, "n", 1, "Number of passwords to generate") 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.excludeChars, "E", "", "Exclude list of characters from generated password")
flag.StringVar(&config.newStyleModes, "M", "", flag.StringVar(&config.newStyleModes, "M", "",
"New style password parameters (higher priority than single parameters)") "New style password parameters (higher priority than single parameters)")
@ -60,15 +60,16 @@ func parseParams(config *Config) {
// Get the password length from the given cli flags // Get the password length from the given cli flags
func getPwLengthFromParams(config *Config) int { func getPwLengthFromParams(config *Config) int {
pwLength := config.minPassLen if config.minPassLen > config.maxPassLen {
if pwLength < config.minPassLen { config.maxPassLen = config.minPassLen
pwLength = config.minPassLen
} }
if pwLength > config.maxPassLen { lenDiff := config.maxPassLen - config.minPassLen + 1
pwLength = config.maxPassLen randAdd, err := getRandNum(lenDiff)
if err != nil {
log.Fatalf("Failed to generated password length: %v", err)
} }
return pwLength return config.minPassLen + randAdd
} }
// Parse the new style parameters // Parse the new style parameters

2
go.mod
View file

@ -1,3 +1,3 @@
module github.com/wneessen/apg.go module github.com/wneessen/apg-go
go 1.16 go 1.16