diff --git a/README.md b/README.md index c8d36d7..78afc77 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,8 @@ # pwgen-go -Password generator practice in Go. It is inspired by https://github.com/jbernard/pwgen +Random password generator practice in Go. It is inspired by https://github.com/jbernard/pwgen ![Go](https://github.com/vuon9/pwgen-go/workflows/Go/badge.svg) -## The manual - -This is following pwgen's manual: https://linux.die.net/man/1/pwgen -or can follow the usages by `pwgen-go -help` or `pwgen-go -h`. - ## Download ```bash @@ -35,7 +30,7 @@ Options supported by pwgen-go: Remove characters from the set of characters to generate passwords -H or -sha1=path/to/file[#seed] Use sha1 hash of given file as a (not so) random generator - -B or -ambigous + -B or -ambiguous Don't include ambiguous characters in the password -v or -no-vowels Do not use any vowels so as to avoid accidental nasty words diff --git a/main.go b/main.go index aa27ebf..6c9f61a 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,6 @@ import ( ) const ( - // TODO: Investigate how to use these below - CONSONANT = 0x0001 - VOWEL = 0x0002 - DIPTHONG = 0x0004 - NOT_FIRST = 0x0008 - PW_DIGITS = 1 PW_UPPERS = 2 PW_SYMBOLS = 4 @@ -69,7 +63,7 @@ func filterValidArgs(allOsArgs []string) []int { return validArgs } -func getOptions(pwArgs []int, pwOptions *pwOptions) *pwOptions { +func getOptions(pwArgs []int, pwOptions *pwOptions) (*pwOptions, error) { if len(pwArgs) >= 1 { pwOptions.pwLen = pwArgs[0] } @@ -78,7 +72,11 @@ func getOptions(pwArgs []int, pwOptions *pwOptions) *pwOptions { pwOptions.numPw = pwArgs[1] } - return pwOptions + if pwOptions.numPw < 1 || pwOptions.pwLen < 5 { + return nil, errors.New("The number of password should be >= 1 and the length of password shoul be >= 5") + } + + return pwOptions, nil } func main() { @@ -90,7 +88,7 @@ func main() { cmdSymbol := "symbol" cmdRemoveChars := "remove-chars" cmdSha1 := "sha1" - cmdAmbigous := "ambigous" + cmdAmbiguous := "ambiguous" cmdNoVowels := "no-vowels" cmdSecure := "secure" cmdColumn := "column" @@ -107,7 +105,7 @@ func main() { NewBoolCommand(Option{cmdSymbol, "y", "", "Include at least one special symbol in the password"}), NewStringCommand(Option{cmdRemoveChars, "r", "-r or --remove-chars=", "Remove characters from the set of characters to generate passwords"}), NewStringCommand(Option{cmdSha1, "H", "-H or -sha1=path/to/file[#seed]", "Use sha1 hash of given file as a (not so) random generator"}), - NewBoolCommand(Option{cmdAmbigous, "B", "", "Don't include ambiguous characters in the password"}), + NewBoolCommand(Option{cmdAmbiguous, "B", "", "Don't include ambiguous characters in the password"}), NewBoolCommand(Option{cmdNoVowels, "v", "", "Do not use any vowels so as to avoid accidental nasty words"}), NewBoolCommand(Option{cmdSecure, "s", "", "Generate completely random passwords"}), NewBoolCommand(Option{cmdColumn, "", "", "Print the generated passwords in columns"}), @@ -132,10 +130,11 @@ func main() { os.Exit(0) } - pwOptions := getOptions( - filterValidArgs(os.Args[0:]), - defaultPwOptions(), - ) + pwOptions, err := getOptions(filterValidArgs(os.Args[0:]), defaultPwOptions()) + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } var pwFlags byte var withColumn bool @@ -155,7 +154,7 @@ func main() { pwFlags = PW_DIGITS | PW_UPPERS case commands.GetBool(cmdSymbol): pwFlags |= PW_SYMBOLS - case commands.GetBool(cmdAmbigous): + case commands.GetBool(cmdAmbiguous): pwFlags |= PW_AMBIGUOUS case commands.GetBool(cmdNoVowels): pwFlags |= PW_NO_VOWELS | PW_DIGITS | PW_UPPERS @@ -219,15 +218,6 @@ func sha1File(filePath string, seed string) { fmt.Printf("%x\n", bs) } -func randomize(size int, chars string) []byte { - newPw := make([]byte, size) - for i := range newPw { - newPw[i] = chars[rand.Int63()%int64(len(chars))] - } - - return newPw -} - func eligibleChars(pwFlags byte, removeChars string) string { chars := pwLowers if (pwFlags & PW_DIGITS) != 0 { @@ -262,6 +252,15 @@ func pwRand(buf *string, pwOptions *pwOptions, chars string) ([]string, error) { return nil, errors.New("no available chars for generating password") } + randomize := func(size int, chars string) []byte { + newPw := make([]byte, size) + for i := range newPw { + newPw[i] = chars[rand.Int63()%int64(len(chars))] + } + + return newPw + } + rand.Seed(time.Now().UnixNano()) var wg sync.WaitGroup