Debian Codegraph @

crypto/kdf/phc.go

raw symbols SCIP
1 package kdf
2
3 import (
4 "encoding/base64"
5 "strconv"
6 "strings"
7
8 "github.com/pkg/errors"
9 )
10
11 // phcEncoding is the alphabet used to encode/decode the hashes. It's based on
12 // the PHC string format:
13 //
14 // https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md
15 var phcEncoding = base64.RawStdEncoding
16
17 // phcAtoi returns the number in the string value or n if value is empty.
18 func phcAtoi(value string, n int) (int, error) {
19 if value == "" {
20 return n, nil
21 }
22 return strconv.Atoi(value)
23 }
24
25 // phcParamsToMap parses the parameters in the string s and returns them in a
26 // map of keys and values.
27 func phcParamsToMap(s string) map[string]string {
28 parameters := strings.Split(s, ",")
29 m := make(map[string]string, len(parameters))
30 for _, p := range parameters {
31 subs := strings.SplitN(p, "=", 2)
32 if len(subs) == 2 {
33 m[subs[0]] = subs[1]
34 } else {
35 m[subs[0]] = ""
36 }
37 }
38 return m
39 }
40
41 // phcEncode creates a string using the PHC format.
42 func phcEncode(identifier, params string, salt, hash []byte) string {
43 ret := "$" + identifier
44 if len(params) > 0 {
45 ret += "$" + params
46 }
47 if len(salt) > 0 {
48 ret += "$" + phcEncoding.EncodeToString(salt)
49 }
50 if len(hash) > 0 {
51 ret += "$" + phcEncoding.EncodeToString(hash)
52 }
53 return ret
54 }
55
56 // phcDecode returns the different parts of a PHC encoded string.
57 func phcDecode(s string) (id string, version int, params string, salt []byte, hash []byte, err error) {
58 subs := strings.SplitN(s, "$", 6)
59 if subs[0] != "" || len(subs) < 2 || (subs[1] == bcryptHash && len(subs) != 4) {
60 return "", 0, "", nil, nil, errors.New("cannot decode password hash")
61 }
62
63 // Special case for bcrypt
64 // return just the id and the full hash
65 if subs[1] == bcryptHash {
66 return bcryptHash, 0, "", nil, []byte(s), nil
67 }
68
69 switch len(subs) {
70 case 6: // id + version + params + salt + hash
71 // version: v=<dec>
72 m := phcParamsToMap(subs[2])
73 if version, err = phcAtoi(m["v"], 0); err != nil {
74 return "", 0, "", nil, nil, err
75 }
76 if hash, err = phcEncoding.DecodeString(subs[5]); err != nil {
77 return "", 0, "", nil, nil, err
78 }
79 if salt, err = phcEncoding.DecodeString(subs[4]); err != nil {
80 return "", 0, "", nil, nil, err
81 }
82 id, params = subs[1], subs[3]
83 case 5: // id + params + salt + hash
84 if hash, err = phcEncoding.DecodeString(subs[4]); err != nil {
85 return "", 0, "", nil, nil, err
86 }
87 if salt, err = phcEncoding.DecodeString(subs[3]); err != nil {
88 return "", 0, "", nil, nil, err
89 }
90 id, params = subs[1], subs[2]
91 case 4: // id + salt + hash
92 if hash, err = phcEncoding.DecodeString(subs[3]); err != nil {
93 return "", 0, "", nil, nil, err
94 }
95 if salt, err = phcEncoding.DecodeString(subs[2]); err != nil {
96 return "", 0, "", nil, nil, err
97 }
98 id = subs[1]
99 case 3: // id + params
100 id, params = subs[1], subs[2]
101 case 2: // id
102 id = subs[1]
103 default:
104 return "", 0, "", nil, nil, errors.New("cannot decode password hash")
105 }
106
107 return
108 }