-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpassgo.go
129 lines (112 loc) · 2.74 KB
/
passgo.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package passgo provides functions for generating human-readable passwords.
package passgo
import (
"bytes"
"errors"
"math/rand"
"time"
)
type Generator struct {
Consonants []byte
Vowels []byte
Numbers []byte
SpecialChars []byte
Capitalize bool
CapitalizeOdds int
buffer bytes.Buffer
}
func (g *Generator) WriteChar(slice []byte) error {
rand.Seed(time.Now().UTC().UnixNano())
n := rand.Intn(len(slice))
if g.Capitalize {
g.buffer.WriteByte(g.ToUpper([]byte{slice[n]}))
} else {
g.buffer.WriteByte(slice[n])
}
return nil
}
func (g *Generator) ToUpper(char []byte) byte {
var b byte
rand.Seed(time.Now().UTC().UnixNano())
n := rand.Intn(g.CapitalizeOdds)
if n == g.CapitalizeOdds-1 {
b = bytes.ToUpper(char)[0]
} else {
b = char[0]
}
return b
}
func (g *Generator) WriteWord(wlen int) error {
for i := 0; i < wlen; i++ {
if i%2 == 0 {
g.WriteChar(g.Consonants)
} else {
g.WriteChar(g.Vowels)
}
}
return nil
}
func (g *Generator) WriteNums(nLen int) error {
for i := 0; i < nLen; i++ {
g.WriteChar(g.Numbers)
}
return nil
}
func (g *Generator) WriteSpecialChars(sLen int) error {
for i := 0; i < sLen; i++ {
g.WriteChar(g.SpecialChars)
}
return nil
}
func (g *Generator) NewPassword(pLen, nLen, sLen int) (string, error) {
if pLen <= 0 {
err := errors.New("Passwords must be at least one character long.")
return "", err
}
if len(g.Consonants) == 0 {
err := errors.New("You must provide some consonants.")
return "", err
}
if len(g.Vowels) == 0 {
err := errors.New("You must provide some vowels.")
return "", err
}
pLen = pLen - (nLen + sLen)
if pLen%2 != 0 {
g.WriteWord(pLen/2 + 1)
} else {
g.WriteWord(pLen / 2)
}
if len(g.Numbers) > 0 {
g.WriteNums(nLen)
}
g.WriteWord(pLen / 2)
if len(g.SpecialChars) > 0 {
g.WriteSpecialChars(sLen)
}
s := g.buffer.String()
g.buffer.Reset()
return s, nil
}
func NewGenerator(cons, vows, nums, specs []byte, caps bool, odds int) (g *Generator) {
g = new(Generator)
g.Consonants = cons
g.Vowels = vows
g.Numbers = nums
g.SpecialChars = specs
g.Capitalize = caps
g.CapitalizeOdds = odds
return
}