Skip to content

Commit

Permalink
Implement crypto/rand instead of math/rand and fixed lower/upper lett…
Browse files Browse the repository at this point in the history
…er swap
  • Loading branch information
Bollos00 committed Dec 19, 2021
1 parent 460a0ac commit 87d9578
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/passwgen/passwgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
package passwgen

import (
"crypto/rand"
"fmt"
"math/rand"
"time"
"math/big"

"github.com/atotto/clipboard"
)
Expand Down Expand Up @@ -134,10 +134,10 @@ func (psg PasswGen) Generate() string {
availableChars = append(availableChars, NumeralChars()...)
}
if psg.CharactersType&UpperLetter != 0 {
availableChars = append(availableChars, LowerLetterChars()...)
availableChars = append(availableChars, UpperLetterChars()...)
}
if psg.CharactersType&LowerLetter != 0 {
availableChars = append(availableChars, UpperLetterChars()...)
availableChars = append(availableChars, LowerLetterChars()...)
}
if psg.CharactersType&Special != 0 {
availableChars = append(availableChars, SpecialChars()...)
Expand All @@ -152,15 +152,16 @@ func (psg PasswGen) Generate() string {

fmt.Printf("Available characters: %c\n\n", availableChars)

rand.Seed(time.Now().UnixNano())
v, _ := rand.Int(rand.Reader, big.NewInt(int64(psg.MaxSize-psg.MinSize+1)))

password_size := psg.MinSize + rand.Intn(psg.MaxSize-psg.MinSize+1)
password_size := psg.MinSize + int(v.Int64())
fmt.Printf("Password size: %v\n\n", password_size)

password := make([]byte, password_size)

for i := range password {
password[i] = availableChars[rand.Intn(int(len(availableChars)))]
vc, _ := rand.Int(rand.Reader, big.NewInt(int64(len(availableChars))))
password[i] = availableChars[int(vc.Int64())]
}

if psg.CopyClipboard {
Expand Down

0 comments on commit 87d9578

Please sign in to comment.