-
Notifications
You must be signed in to change notification settings - Fork 4
/
sgp.go
92 lines (74 loc) · 1.99 KB
/
sgp.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
package main
import "hash"
const (
washRounds = 10
minPasswordLength = 4
)
type SGP interface {
Hasher() hash.Hash
MaxLength() int
ZeroBytes()
PwBuf() []byte // used by generatePass
HashBuf() []byte // used by Generate()
FixPadding([]byte)
}
func SupergenPass(out []byte, hasher SGP, password, domain []byte) error {
return generatePass(out, hasher, password, []byte(":"), domain)
}
func generatePass(out []byte, sgp SGP, pw_parts ...[]byte) error {
if len(out) > sgp.MaxLength() {
return errorRequestTooLong(len(out), sgp.MaxLength())
}
pw := sgp.PwBuf()
digest := sgp.HashBuf()
defer zeroBytes(digest)
hashSlices(digest, sgp.Hasher(), pw_parts...)
sgpBase64(pw, digest, _SGP_BASE64_ALPHABET)
sgp.FixPadding(pw)
for round := 1; round < washRounds; round += 1 {
hashSlices(digest, sgp.Hasher(), pw)
sgpBase64(pw, digest, _SGP_BASE64_ALPHABET)
sgp.FixPadding(pw)
}
for !passwordIsValid(pw[:len(out)]) {
hashSlices(digest, sgp.Hasher(), pw)
sgpBase64(pw, digest, _SGP_BASE64_ALPHABET)
sgp.FixPadding(pw)
}
copy(out, pw)
return nil
}
// returns true only if:
//
// 1. 'password' must start with a lowercase letter [a-z].
// 2. 'password' must contain at least one uppercase letter [A-Z].
// 3. 'password' must contain at least one numeral [0-9].
//
// see 'var validatePassword = function (str, length) { ... }' in
// github.com/chriszarate/supergenpass-lib/blob/master/supergenpass-lib.js
func passwordIsValid(password []byte) bool {
if len(password) == 0 {
return false
}
if !(password[0] >= 'a' && password[0] <= 'z') {
return false
}
var hasDigit, hasLetter bool
for i := 0; !(hasDigit && hasLetter) && i < len(password); i++ {
c := password[i]
if c >= '0' && c <= '9' {
hasDigit = true
} else if c >= 'A' && c <= 'Z' {
hasLetter = true
}
}
return (hasDigit && hasLetter)
}
func hashSlices(out []byte, hasher hash.Hash, slices ...[]byte) {
defer hasher.Reset()
hasher.Reset()
for i := range slices {
hasher.Write(slices[i])
}
hasher.Sum(out)
}