This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
/
password_encoder_scrypt.go
145 lines (113 loc) · 3.05 KB
/
password_encoder_scrypt.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package security
import (
"strings"
)
type ScryptPasswordEncoderOption func(encoder *ScryptPasswordEncoder)
type ScryptPasswordEncoder struct {
N int
r int
p int
saltLength int
keyLength int
}
func NewScryptPasswordEncoder(options ...ScryptPasswordEncoderOption) *ScryptPasswordEncoder {
encoder := &ScryptPasswordEncoder{
N: 32768,
r: 8,
p: 1,
saltLength: 16,
keyLength: 32,
}
for _, opt := range options {
opt(encoder)
}
return encoder
}
func WithScryptN(N int) ScryptPasswordEncoderOption {
return func(encoder *ScryptPasswordEncoder) {
encoder.N = N
}
}
func WithScryptR(r int) ScryptPasswordEncoderOption {
return func(encoder *ScryptPasswordEncoder) {
encoder.r = r
}
}
func WithScryptP(p int) ScryptPasswordEncoderOption {
return func(encoder *ScryptPasswordEncoder) {
encoder.p = p
}
}
func WithScryptSaltLength(saltLength int) ScryptPasswordEncoderOption {
return func(encoder *ScryptPasswordEncoder) {
encoder.saltLength = saltLength
}
}
func WithScryptKeyLength(keyLength int) ScryptPasswordEncoderOption {
return func(encoder *ScryptPasswordEncoder) {
encoder.keyLength = keyLength
}
}
func (encoder *ScryptPasswordEncoder) Encode(rawPassword string) (*string, error) {
var err error
var salt []byte
if salt, err = GenerateSalt(encoder.saltLength); err != nil {
return nil, err
}
var value *string
if value, err = ScryptEncode(rawPassword, salt, encoder.N, encoder.r, encoder.p, encoder.keyLength); err != nil {
return nil, err
}
encodedPassword := *value
encodedPassword = ScryptPrefixKey + encodedPassword
return &encodedPassword, nil
}
func (encoder *ScryptPasswordEncoder) Matches(encodedPassword string, rawPassword string) (*bool, error) {
if rawPassword == "" {
return nil, ErrRawPasswordIsEmpty
}
if !strings.HasPrefix(encodedPassword, ScryptPrefixKey) {
return nil, ErrEncodedPasswordNotAllowed
}
_, N, r, p, salt, key, err := ScryptDecode(encodedPassword)
if err != nil {
return nil, err
}
var newEncodedPassword *string
if newEncodedPassword, err = ScryptEncode(rawPassword, salt, *N, *r, *p, len(key)); err != nil {
return nil, err
}
encodedPassword = strings.Replace(encodedPassword, ScryptPrefixKey, "", 1)
matched := encodedPassword == *(newEncodedPassword)
return &matched, nil
}
func (encoder *ScryptPasswordEncoder) UpgradeEncoding(encodedPassword string) (*bool, error) {
if encodedPassword == "" {
return nil, ErrEncodedPasswordIsEmpty
}
if !strings.HasPrefix(encodedPassword, ScryptPrefixKey) {
return nil, ErrEncodedPasswordNotAllowed
}
_, N, r, p, salt, key, err := ScryptDecode(encodedPassword)
if err != nil {
return nil, err
}
upgradeNeeded := true
if encoder.N > *(N) {
return &upgradeNeeded, nil
}
if encoder.r > *(r) {
return &upgradeNeeded, nil
}
if encoder.p > *(p) {
return &upgradeNeeded, nil
}
if encoder.saltLength > len(salt) {
return &upgradeNeeded, nil
}
if encoder.keyLength > len(key) {
return &upgradeNeeded, nil
}
upgradeNeeded = false
return &upgradeNeeded, nil
}