-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
103 lines (95 loc) · 2.49 KB
/
settings.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
// Copyright 2018 Philipp Brüll ([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package crypt
import (
"regexp"
"strconv"
"strings"
)
var (
roundsPrefix = []byte("rounds=")
costRegexp = regexp.MustCompile(`^[0-9]{2}$`)
)
// Parameter defines a parameter map.
type Parameter map[string]string
// GetInt tries to return an int value from the parameter map and returns the provided
// defaultValue on failure.
func (p Parameter) GetInt(key string, defaultValue int) int {
if p == nil {
return defaultValue
}
text, found := p[key]
if !found {
return defaultValue
}
value, _ := strconv.Atoi(text)
return value
}
// Settings removes the hash from the provided settings and returns the result.
func Settings(settings string) string {
parts := strings.Split(settings, "$")
keep := 3
if len(parts) > 3 {
if s := parts[2]; strings.Contains(s, "=") || costRegexp.MatchString(s) {
keep++
}
}
if len(parts) > 4 {
if s := parts[3]; strings.Contains(s, "=") || costRegexp.MatchString(s) {
keep++
}
}
return strings.Join(parts[:keep], "$")
}
// DecodeSettings decodes the provided settings string into it's parts.
func DecodeSettings(settings string) (code string, parameter Parameter, salt string, hash string, err error) {
step := 0
for _, part := range strings.Split(settings, "$") {
switch step {
case 0, 1:
code = part
step++
case 2:
if strings.Contains(part, "=") {
if parameter == nil {
parameter = Parameter{}
}
parameter = decodeParameters(parameter, part)
} else if costRegexp.MatchString(part) {
if parameter == nil {
parameter = Parameter{}
}
parameter["cost"] = strings.Trim(part, "0")
} else {
salt = part
step++
}
case 3:
hash = part
step++
}
}
return
}
func decodeParameters(p Parameter, text string) Parameter {
pairs := strings.Split(text, ",")
for _, pair := range pairs {
parts := strings.SplitN(pair, "=", 2)
if len(parts) < 2 {
continue
}
p[parts[0]] = parts[1]
}
return p
}