-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspectre.go
83 lines (70 loc) · 2.5 KB
/
spectre.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
package passit
import (
"errors"
"io"
"strings"
)
// SpectreTemplate is a Generator that implements a variant of the
// Spectre / Master Password encoding algorithm by Maarten Billemont.
//
// This algorithm is not compatible with any of the officially published algorithms,
// but it does produce passwords using the same templates that are indistinguishable
// from the official algorithm. Unlike that algorithm, this doesn't exhibit a modulo
// bias.
type SpectreTemplate string
// These are the standard templates defined by Spectre / Master Password.
const (
SpectreMaximum SpectreTemplate = "anoxxxxxxxxxxxxxxxxx:axxxxxxxxxxxxxxxxxno"
SpectreLong SpectreTemplate = "CvcvnoCvcvCvcv:CvcvCvcvnoCvcv:CvcvCvcvCvcvno:CvccnoCvcvCvcv:CvccCvcvnoCvcv:CvccCvcvCvcvno:CvcvnoCvccCvcv:CvcvCvccnoCvcv:CvcvCvccCvcvno:CvcvnoCvcvCvcc:CvcvCvcvnoCvcc:CvcvCvcvCvccno:CvccnoCvccCvcv:CvccCvccnoCvcv:CvccCvccCvcvno:CvcvnoCvccCvcc:CvcvCvccnoCvcc:CvcvCvccCvccno:CvccnoCvcvCvcc:CvccCvcvnoCvcc:CvccCvcvCvccno"
SpectreMedium SpectreTemplate = "CvcnoCvc:CvcCvcno"
SpectreBasic SpectreTemplate = "aaanaaan:aannaaan:aaannaaa"
SpectreShort SpectreTemplate = "Cvcn"
SpectrePIN SpectreTemplate = "nnnn"
SpectreName SpectreTemplate = "cvccvcvcv"
SpectrePhrase SpectreTemplate = "cvcc cvc cvccvcv cvc:cvc cvccvcvcv cvcv:cv cvccv cvc cvcvccv"
)
func (st SpectreTemplate) readTemplate(r io.Reader) (string, error) {
templates := string(st)
n, err := readIntN(r, strings.Count(templates, ":")+1)
if err != nil {
return "", err
}
for range n {
_, templates, _ = strings.Cut(templates, ":")
}
template, _, _ := strings.Cut(templates, ":")
return template, nil
}
// Password implements Generator.
func (st SpectreTemplate) Password(r io.Reader) (string, error) {
template, err := st.readTemplate(r)
if err != nil {
return "", err
}
var sb strings.Builder
sb.Grow(len(template))
for _, c := range []byte(template) {
chars, ok := spectreChars[c]
if !ok {
return "", errors.New("passit: template contains invalid character")
}
n, err := readIntN(r, len(chars))
if err != nil {
return "", err
}
sb.WriteByte(chars[n])
}
return sb.String(), nil
}
var spectreChars = map[byte]string{
'V': "AEIOU",
'C': "BCDFGHJKLMNPQRSTVWXYZ",
'v': "aeiou",
'c': "bcdfghjklmnpqrstvwxyz",
'A': "AEIOUBCDFGHJKLMNPQRSTVWXYZ",
'a': "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz",
'n': "0123456789",
'o': "@&%?,=[]_:-+*$#!'^~;()/.",
'x': "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz0123456789!@#$%^&*()",
' ': " ",
}