Skip to content

Commit

Permalink
- Remove phonemes-related stuffs, fix typo
Browse files Browse the repository at this point in the history
- Check len & number of pwd
  • Loading branch information
vuon9 committed Dec 13, 2020
1 parent 5cf68a8 commit 3cd73b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
47 changes: 23 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
}
Expand All @@ -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() {
Expand All @@ -90,7 +88,7 @@ func main() {
cmdSymbol := "symbol"
cmdRemoveChars := "remove-chars"
cmdSha1 := "sha1"
cmdAmbigous := "ambigous"
cmdAmbiguous := "ambiguous"
cmdNoVowels := "no-vowels"
cmdSecure := "secure"
cmdColumn := "column"
Expand All @@ -107,7 +105,7 @@ func main() {
NewBoolCommand(Option{cmdSymbol, "y", "", "Include at least one special symbol in the password"}),
NewStringCommand(Option{cmdRemoveChars, "r", "-r <chars> or --remove-chars=<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"}),
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3cd73b7

Please sign in to comment.