Merge pull request #14 from wneessen/dev

v0.3.1: New password length behaviour
This commit is contained in:
Winni Neessen 2021-04-17 11:21:44 +02:00 committed by GitHub
commit 3e001035f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 45 deletions

View file

@ -4,10 +4,10 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<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="For some reason, the tests on GH fail">
<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_test.go" beforeDir="false" afterPath="$PROJECT_DIR$/apg_test.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$/config.go" beforeDir="false" afterPath="$PROJECT_DIR$/config.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 +49,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 +70,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 +86,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 +96,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 +106,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 +116,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 +136,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>
@ -232,7 +231,9 @@
<MESSAGE value="New DEV branch" /> <MESSAGE value="New DEV branch" />
<MESSAGE value="v0.2.9: Replaced standard go-help with custom usage text" /> <MESSAGE value="v0.2.9: Replaced standard go-help with custom usage text" />
<MESSAGE value="v0.3.0: Unified the naming convention" /> <MESSAGE value="v0.3.0: Unified the naming convention" />
<option name="LAST_COMMIT_MESSAGE" value="v0.3.0: Unified the naming convention" /> <MESSAGE value="v0.3.1: New password length behaviour&#10;&#10;To address issue #13, the password length behaviour of the &#10;original APG has been reproduced. Previously, when a minLength&#10;of 5 and a maxLength of 10 was given, apg-go se the pwLength to&#10;the preferred maxLength.&#10;&#10;With v0.3.1 it will choose a random length between minLength and&#10;maxLength instead, same as the original C-lang apg did. For this&#10;the minLength has been defaulted to a sane value of 12 (instead &#10;of the 8 of the original apg). The default for maxLength stayed&#10;at 20.&#10;&#10;Also the default number of generated passwords has been changed &#10;from 1 to 6, to replicate the behaviour of the original apg." />
<MESSAGE value="For some reason, the tests on GH fail" />
<option name="LAST_COMMIT_MESSAGE" value="For some reason, the tests on GH fail" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<integration-enabled>true</integration-enabled> <integration-enabled>true</integration-enabled>

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)

11
apg.go
View file

@ -8,8 +8,9 @@ import (
) )
// Constants // Constants
const DefaultPwLenght int = 20 const DefaultMinLenght int = 12
const VersionString string = "0.3.0" const DefaultMaxLenght int = 20
const VersionString string = "0.3.1"
type Config struct { type Config struct {
minPassLen int minPassLen int
@ -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] [-l] [-M mode] [-E char_string] [-n num_of_pass] [-v] [-h]
Options: 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) -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 -E CHARS List of characters to be excluded in the generated password
-M [LUNSHClunshc] New style password parameters (upper case: on, lower case: off) -M [LUNSHClunshc] New style password parameters (upper case: on, lower case: off)
-L Use lower case characters in passwords (Default: on) -L Use lower case characters in passwords (Default: on)
@ -69,11 +70,11 @@ func main() {
} }
// Set PW length and available characterset // Set PW length and available characterset
pwLength := getPwLengthFromParams(&config)
charRange := getCharRange(&config) charRange := getCharRange(&config)
// Generate passwords // Generate passwords
for i := 1; i <= config.numOfPass; i++ { for i := 1; i <= config.numOfPass; i++ {
pwLength := getPwLengthFromParams(&config)
pwString, err := getRandChar(&charRange, pwLength) pwString, err := getRandChar(&charRange, pwLength)
if err != nil { if err != nil {
log.Fatalf("getRandChar returned an error: %q\n", err) log.Fatalf("getRandChar returned an error: %q\n", err)

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,20 @@ 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)
}
retVal := config.minPassLen + randAdd
if retVal <= 0 {
return 1
} }
return pwLength return retVal
} }
// 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