Skip to content

Commit 5631982

Browse files
committed
Support creating word-ish password
This adds the NewWordish() func
1 parent 4cb17a6 commit 5631982

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

passgen/passgen.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package passgen generates random, unmemorable passwords.
1+
// Package passgen generates random passwords.
22
//
33
// Example usage:
44
//
@@ -19,20 +19,20 @@ const DefLen = 20
1919
// DefChars is the default set of characters used in the password.
2020
var DefChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()- _=+,.?/:;{}[]`~")
2121

22-
// New returns a random password of the default length with the default set of
23-
// characters.
22+
// New returns a random, unmemorable password of the default length with the
23+
// default set of characters.
2424
func New() string {
2525
return NewLenChars(DefLen, DefChars)
2626
}
2727

28-
// NewLen returns a random password of the given length with the default set
29-
// of characters.
28+
// NewLen returns a random, unmemorable password of the given length with the
29+
// default set of characters.
3030
func NewLen(length int) string {
3131
return NewLenChars(length, DefChars)
3232
}
3333

34-
// NewLenChars returns a random password of the given length with the given
35-
// set of characters.
34+
// NewLenChars returns a random, unmemorable password of the given length with
35+
// the given set of characters.
3636
func NewLenChars(length int, chars []byte) string {
3737
if length == 0 {
3838
return ""

passgen/wordish.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package passgen
2+
3+
import (
4+
"crypto/rand"
5+
"math/big"
6+
)
7+
8+
var (
9+
ar = []rune("aA4")
10+
cr = []rune("cC")
11+
er = []rune("eE3")
12+
fr = []rune("fF")
13+
gr = []rune("gG")
14+
hr = []rune("hH")
15+
ir = []rune("iI1")
16+
lr = []rune("lL")
17+
nr = []rune("nN")
18+
or = []rune("oO0")
19+
rr = []rune("rR")
20+
sr = []rune("sS5")
21+
tr = []rune("tT7")
22+
remr = []rune("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ0123456789")
23+
)
24+
25+
// NewWordish generates a password made of word-like words.
26+
func NewWordish() string {
27+
b := []rune{}
28+
b = append(b, randLetter(cr))
29+
b = append(b, randLetter(hr))
30+
b = append(b, randLetter(ar))
31+
b = append(b, randLetter(nr))
32+
b = append(b, randLetter(gr))
33+
b = append(b, randLetter(er))
34+
b = append(b, randLetter(tr))
35+
b = append(b, randLetter(hr))
36+
b = append(b, randLetter(ir))
37+
b = append(b, randLetter(sr))
38+
b = append(b, randLetter(ar))
39+
b = append(b, randLetter(fr))
40+
b = append(b, randLetter(tr))
41+
b = append(b, randLetter(er))
42+
b = append(b, randLetter(rr))
43+
b = append(b, randLetter(lr))
44+
b = append(b, randLetter(or))
45+
b = append(b, randLetter(gr))
46+
b = append(b, randLetter(gr))
47+
b = append(b, randLetter(ir))
48+
b = append(b, randLetter(nr))
49+
b = append(b, randLetter(gr))
50+
b = append(b, randLetter(ir))
51+
b = append(b, randLetter(nr))
52+
for i := 0; i <= 7; i++ {
53+
b = append(b, randLetter(remr))
54+
}
55+
return string(b)
56+
}
57+
58+
func randLetter(l []rune) rune {
59+
li, err := rand.Int(rand.Reader, big.NewInt(int64(len(l))))
60+
if err != nil {
61+
return rune(-1)
62+
}
63+
return l[li.Int64()]
64+
}

0 commit comments

Comments
 (0)