-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
80 lines (69 loc) · 1.48 KB
/
parse.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
// +build go1.11
package passwd
import (
"strings"
"unicode"
"golang.org/x/text/unicode/rangetable"
)
const (
separatorRune = rune('$')
)
var (
rangeTableSeparator = rangetable.New(separatorRune)
)
func token(c rune) bool {
return unicode.Is(rangeTableSeparator, c)
}
func parseFromHashToParams(hashed []byte) (interface{}, error) {
fields := strings.FieldsFunc(string(hashed), token)
if len(fields) < 2 {
return nil, ErrParse
}
switch fields[0] {
case idBcrypt:
bp, err := newBcryptParamsFromHash(hashed)
if err != nil {
return nil, err
}
return *bp, nil
case idScrypt:
//fmt.Printf("scrypt compare!\n")
sp, err := newScryptParamsFromFields(fields[1:]) // mismatch.
if err != nil {
// XXX wrapp the error
return nil, err
}
return *sp, nil
case idArgon2i:
fallthrough
case idArgon2id:
ap, err := newArgon2ParamsFromFields(fields[1:]) // mismatch.
if err != nil {
// XXX wrapp the error
return nil, err
}
return *ap, nil
}
return nil, ErrParse
}
func parseFromHashToSalt(hashed []byte) ([]byte, error) {
fields := strings.FieldsFunc(string(hashed), token)
if len(fields) < 2 {
return nil, ErrParse
}
switch fields[0] {
case idBcrypt:
return nil, nil
case idScrypt:
fallthrough
case idArgon2i:
fallthrough
case idArgon2id: // with different salt len it might have matched.
salt, err := base64Decode([]byte(fields[1])) // process the salt
if err != nil {
return nil, ErrParse
}
return salt, nil
}
return nil, ErrParse
}