-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgen.go
More file actions
35 lines (31 loc) · 718 Bytes
/
gen.go
File metadata and controls
35 lines (31 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"crypto/rand"
"io"
)
var stdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+,.?/:;{}[]`~")
func newPassword() string {
return randChar(8, stdChars)
}
func randChar(length int, chars []byte) string {
newPword := make([]byte, length)
randomData := make([]byte, length+(length/4)) // storage for random bytes.
clen := byte(len(chars))
maxrb := byte(256 - (256 % len(chars)))
i := 0
for {
if _, err := io.ReadFull(rand.Reader, randomData); err != nil {
panic(err)
}
for _, c := range randomData {
if c >= maxrb {
continue
}
newPword[i] = chars[c%clen]
i++
if i == length {
return string(newPword)
}
}
}
}