-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
242 lines (202 loc) · 4.06 KB
/
helpers.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
package password // import "github.com/nathanaelle/password/v2"
import (
"crypto/rand"
"encoding/base64"
"fmt"
"hash"
"strconv"
"strings"
)
var rawbase64 = base64.RawStdEncoding
var bc64 = base64.NewEncoding("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789").WithPadding(base64.NoPadding)
func getrand(l int) []byte {
if l < 1 || l > 65536 {
panic("get rand size <l> is not valid")
}
ret := make([]byte, l)
_, err := rand.Read(ret)
if err != nil {
panic(err)
}
return ret
}
func getrandh64(l int) []byte {
if l < 1 || l > 65536 {
panic("get rand size <l> is not valid")
}
t := make([]byte, l)
_, err := rand.Read(t)
if err != nil {
panic(err)
}
ret := h64Encode(t)
return ret[:l]
}
func optionsSingleInt(str, optName string, maxLen int) map[string]interface{} {
if str == "" {
return nil
}
if len(str) > maxLen {
return nil
}
v, err := strconv.Atoi(str)
if err != nil {
return nil
}
ret := make(map[string]interface{})
ret[optName] = v
return ret
}
func options(str string) map[string]interface{} {
if str == "" {
return nil
}
if !strings.Contains(str, "=") {
return nil
}
list := strings.Split(str, ",")
ret := make(map[string]interface{})
for _, tok := range list {
kv := strings.SplitN(tok, "=", 2)
if len(kv) == 2 {
ret[kv[0]] = kv[1]
}
}
return ret
}
func optionInt(opt map[string]interface{}, k string, def int) (int, bool) {
if opt == nil {
return def, true
}
ienc, ok := opt[k]
if !ok {
return def, true
}
switch enc := ienc.(type) {
case string:
v, err := strconv.Atoi(enc)
if err != nil {
return 0, false
}
return v, true
case int:
return enc, true
}
return 0, false
}
func assert(t bool, i interface{}) {
if !t {
panic(i)
}
}
func bounded(min, v, max int) int {
if v < min {
return min
}
if v > max {
return max
}
return v
}
func repeatBytes(src []byte, lenDest int) []byte {
dest := make([]byte, 0, lenDest)
lenSrc := len(src)
mod := lenDest % lenSrc
rounds := (lenDest - mod)
if rounds > 0 {
for i := 0; i < rounds; i += lenSrc {
dest = append(dest, src...)
}
}
if mod != 0 {
dest = append(dest, src[0:mod]...)
}
assert(len(dest) == lenDest, fmt.Errorf("lenDest [%d] != len(dest) [%d] lenSrc [%d]", lenDest, len(dest), lenSrc))
return dest
}
func multiplyBytes(src []byte, scalar int) [][]byte {
ret := make([][]byte, scalar)
for i := range ret {
ret[i] = src
}
return ret
}
const (
h64 string = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
)
func h64Encode(src []byte) []byte {
if len(src) == 0 {
return nil
}
hashSize := len(src) * 4 / 3
if (len(src) % 3) != 0 {
hashSize++
}
hash := make([]byte, hashSize)
dst := hash
for len(src) > 0 {
switch len(src) {
default:
dst[0] = h64[src[0]&0x3f]
dst[1] = h64[((src[0]>>6)|(src[1]<<2))&0x3f]
dst[2] = h64[((src[1]>>4)|(src[2]<<4))&0x3f]
dst[3] = h64[(src[2]>>2)&0x3f]
src = src[3:]
dst = dst[4:]
case 2:
dst[0] = h64[src[0]&0x3f]
dst[1] = h64[((src[0]>>6)|(src[1]<<2))&0x3f]
dst[2] = h64[(src[1]>>4)&0x3f]
src = src[2:]
dst = dst[3:]
case 1:
dst[0] = h64[src[0]&0x3f]
dst[1] = h64[(src[0]>>6)&0x3f]
src = src[1:]
dst = dst[2:]
}
}
return hash
}
// used in SHA256-CRYPT SHA512-CRYPT MD5-CRYPT
func commonSum(h hash.Hash, vec ...[]byte) hash.Hash {
for _, s := range vec {
h.Write(s)
}
return h
}
// used in SHA256-CRYPT SHA512-CRYPT
func commonDispatch(i int, sumC, sumP, sumS []byte) [][]byte {
if i%42 == 0 {
return [][]byte{sumC, sumP}
}
if i%21 == 0 {
return [][]byte{sumP, sumC}
}
if i%14 == 0 {
return [][]byte{sumC, sumS, sumP}
}
if i%7 == 0 {
return [][]byte{sumP, sumS, sumC}
}
if i%6 == 0 {
return [][]byte{sumC, sumP, sumP}
}
if i%3 == 0 {
return [][]byte{sumP, sumP, sumC}
}
if i%2 == 0 {
return [][]byte{sumC, sumS, sumP, sumP}
}
return [][]byte{sumP, sumS, sumP, sumC}
}
func commonMixer(l int, caseA, caseB []byte) (ret [][]byte) {
for i := l; i > 0; i >>= 1 {
if (i % 2) != 0 {
ret = append(ret, caseA)
} else {
ret = append(ret, caseB)
}
}
return
}