Skip to content

Commit d4eb373

Browse files
authoredFeb 9, 2023
v0.1.2, 2023-02-09
use "crypto/rand" to create password seed instead of "math/rand"
1 parent 56062b1 commit d4eb373

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed
 

‎passgen.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package main
22

33
import (
4+
"bytes"
5+
r "crypto/rand"
6+
"encoding/binary"
47
"fmt"
58
"math/rand"
69
"os"
710
"os/exec"
811
"runtime"
9-
"time"
1012
)
1113

1214
// version history
1315
// v0.1.0, 2023-01-10; initial release
1416
// v0.1.1, 2023-01-11; added sanity checks to passwordLength & passwordCount inputs
17+
// v0.1.2, 2023-02-09; use "crypto/rand" to create password seed
1518

1619
// clear screen function
1720
func clearScreen() {
@@ -31,13 +34,28 @@ func clearScreen() {
3134
}
3235
}
3336

37+
func generateSeedKey() int64 {
38+
b := make([]byte, 16)
39+
_, err := r.Read(b)
40+
if err != nil {
41+
fmt.Println("Error:", err)
42+
return 0
43+
}
44+
45+
var seed int64
46+
binary.Read(bytes.NewReader(b), binary.BigEndian, &seed)
47+
return seed
48+
}
49+
3450
func RandPassGen(n int) string {
3551
var alphaChars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
3652
var digitChars = []rune("0123456789")
3753
var specialChars = []rune("!@#$%^&*()_+-=[]{}\\|;':\"<>,.?/")
38-
rand.Seed(time.Now().UnixNano())
54+
3955
b := make([]rune, n)
4056
for i := range b {
57+
seed := generateSeedKey()
58+
rand.Seed(seed)
4159
r := rand.Intn(3)
4260
if r == 0 {
4361
b[i] = alphaChars[rand.Intn(len(alphaChars))]

0 commit comments

Comments
 (0)
Please sign in to comment.