-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.go
288 lines (225 loc) · 6.85 KB
/
types.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package crypt
import (
"database/sql/driver"
"fmt"
"github.com/go-crypt/crypt/algorithm"
)
// NewDigest wraps an algorithm.Digest in the convenience layer of the crypt.Digest.
func NewDigest(d algorithm.Digest) (digest *Digest, err error) {
if d == nil {
return nil, fmt.Errorf("can't create crypt.Digest from nil")
}
return &Digest{digest: d}, nil
}
// NewDigestDecode decodes a string into a algorithm.Digest and wraps it in the convenience layer of the crypt.Digest.
func NewDigestDecode(encodedDigest string) (digest *Digest, err error) {
if len(encodedDigest) == 0 {
return nil, fmt.Errorf("can't create crypt.Digest from empty string")
}
var d algorithm.Digest
if d, err = Decode(encodedDigest); err != nil {
return nil, err
}
return &Digest{digest: d}, nil
}
// NewNullDigest wraps an algorithm.Digest in the convenience layer of the crypt.NullDigest.
func NewNullDigest(d algorithm.Digest) (digest *NullDigest) {
return &NullDigest{digest: d}
}
// NewNullDigestDecode decodes a string into a algorithm.Digest and wraps it in the convenience layer of the crypt.NullDigest.
func NewNullDigestDecode(encodedDigest string) (digest *NullDigest, err error) {
if len(encodedDigest) == 0 {
return &NullDigest{}, nil
}
var (
d algorithm.Digest
)
if d, err = Decode(encodedDigest); err != nil {
return nil, err
}
return &NullDigest{digest: d}, nil
}
// Digest is a decorator struct which wraps the algorithm.Digest and adds sql.Scanner/driver.Valuer,
// encoding.TextMarshaler/encoding.TextUnmarshaler, and encoding.BinaryMarshaler/encoding.BinaryUnmarshaler
// implementations.
type Digest struct {
digest algorithm.Digest
}
// Encode decorates the algorithm.Digest Encode function.
func (d *Digest) Encode() string {
return d.digest.Encode()
}
// String decorates the algorithm.Digest String function.
func (d *Digest) String() string {
return d.digest.String()
}
// MatchBytes decorates the algorithm.Digest MatchBytes function.
func (d *Digest) MatchBytes(passwordBytes []byte) (match bool) {
return d.digest.MatchBytes(passwordBytes)
}
// MatchAdvanced decorates the algorithm.Digest MatchAdvanced function.
func (d *Digest) MatchAdvanced(password string) (match bool, err error) {
return d.digest.MatchAdvanced(password)
}
// MatchBytesAdvanced decorates the algorithm.Digest MatchBytesAdvanced function.
func (d *Digest) MatchBytesAdvanced(passwordBytes []byte) (match bool, err error) {
return d.digest.MatchBytesAdvanced(passwordBytes)
}
// Match decorates the algorithm.Digest Match function.
func (d *Digest) Match(password string) (match bool) {
return d.digest.Match(password)
}
// Value implements driver.Valuer.
func (d *Digest) Value() (value driver.Value, err error) {
if d.digest == nil {
return "", nil
}
return d.digest.Encode(), nil
}
// Scan implements sql.Scanner.
func (d *Digest) Scan(src any) (err error) {
switch digest := src.(type) {
case nil:
return fmt.Errorf("invalid type for crypt.Digest: can't scan nil value into crypt.Digest: use crypt.NullDigest instead")
case string:
if d.digest, err = Decode(digest); err != nil {
return err
}
return nil
case byte:
if d.digest, err = Decode(string(digest)); err != nil {
return err
}
return nil
default:
return fmt.Errorf("invalid type for crypt.Digest: can't scan %T into crypt.Digest", digest)
}
}
// MarshalText implements encoding.TextMarshaler.
func (d *Digest) MarshalText() (data []byte, err error) {
if d.digest == nil {
return []byte(""), nil
}
return []byte(d.digest.Encode()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (d *Digest) UnmarshalText(data []byte) (err error) {
if len(data) == 0 {
return fmt.Errorf("can't unmarhsal empty data to crypt.Digest")
}
var digest algorithm.Digest
if digest, err = Decode(string(data)); err != nil {
return err
}
d.digest = digest
return nil
}
// MarshalBinary implements encoding.BinaryMarshaler.
func (d *Digest) MarshalBinary() (data []byte, err error) {
return d.MarshalText()
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (d *Digest) UnmarshalBinary(data []byte) (err error) {
return d.UnmarshalText(data)
}
// NullDigest is variation of crypt.Digest which accepts nulls.
type NullDigest struct {
digest algorithm.Digest
}
// Encode decorates the algorithm.Digest Encode function.
func (d *NullDigest) Encode() string {
if d.digest == nil {
return ""
}
return d.digest.Encode()
}
// String decorates the algorithm.Digest String function.
func (d *NullDigest) String() string {
if d.digest == nil {
return ""
}
return d.digest.String()
}
// Match decorates the algorithm.Digest Match function.
func (d *NullDigest) Match(password string) (match bool) {
if d.digest == nil {
return false
}
return d.digest.Match(password)
}
// MatchBytes decorates the algorithm.Digest MatchBytes function.
func (d *NullDigest) MatchBytes(passwordBytes []byte) (match bool) {
if d.digest == nil {
return false
}
return d.digest.MatchBytes(passwordBytes)
}
// MatchAdvanced decorates the algorithm.Digest MatchAdvanced function.
func (d *NullDigest) MatchAdvanced(password string) (match bool, err error) {
if d.digest == nil {
return false, nil
}
return d.digest.MatchAdvanced(password)
}
// MatchBytesAdvanced decorates the algorithm.Digest MatchBytesAdvanced function.
func (d *NullDigest) MatchBytesAdvanced(passwordBytes []byte) (match bool, err error) {
if d.digest == nil {
return false, nil
}
return d.digest.MatchBytesAdvanced(passwordBytes)
}
// Value implements driver.Valuer.
func (d *NullDigest) Value() (value driver.Value, err error) {
if d.digest == nil {
return nil, nil
}
return d.digest.Encode(), nil
}
// Scan implements sql.Scanner.
func (d *NullDigest) Scan(src any) (err error) {
switch digest := src.(type) {
case nil:
d.digest = nil
return nil
case string:
if d.digest, err = Decode(digest); err != nil {
return err
}
return nil
case byte:
if d.digest, err = Decode(string(digest)); err != nil {
return err
}
return nil
default:
return fmt.Errorf("invalid type for crypt.Digest: can't scan %T into crypt.Digest", digest)
}
}
// MarshalText implements encoding.TextMarshaler.
func (d *NullDigest) MarshalText() (data []byte, err error) {
if d.digest == nil {
return nil, nil
}
return []byte(d.digest.Encode()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (d *NullDigest) UnmarshalText(data []byte) (err error) {
if len(data) == 0 {
d.digest = nil
return nil
}
var digest algorithm.Digest
if digest, err = Decode(string(data)); err != nil {
return err
}
d.digest = digest
return nil
}
// MarshalBinary implements encoding.BinaryMarshaler.
func (d *NullDigest) MarshalBinary() (data []byte, err error) {
return d.MarshalText()
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (d *NullDigest) UnmarshalBinary(data []byte) (err error) {
return d.UnmarshalText(data)
}