-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfunctions_defs_crypto_rand.go
More file actions
317 lines (307 loc) · 8.46 KB
/
functions_defs_crypto_rand.go
File metadata and controls
317 lines (307 loc) · 8.46 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package diecast
import (
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"math"
mrand "math/rand"
"strings"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/typeutil"
"github.com/spaolacci/murmur3"
)
func hashingAlgo(alg string) (func() hash.Hash, error) {
alg = strings.ToLower(alg)
switch alg {
case `sha1`:
return sha1.New, nil
case `sha224`:
return sha256.New224, nil
case `sha256`:
return sha256.New, nil
case `sha384`:
return sha512.New384, nil
case `sha512`:
return sha512.New, nil
case `md5`:
return md5.New, nil
default:
return nil, fmt.Errorf("unknown algorithm %q", alg)
}
}
func hashTheThing(fn string, input any) (string, error) {
if hasher, err := hashingAlgo(fn); err == nil {
var data = []byte(typeutil.String(input))
return hex.EncodeToString(hasher().Sum(data)), nil
} else {
return ``, err
}
}
func loadStandardFunctionsCryptoRand(_ FuncMap, _ *Server) funcGroup {
return funcGroup{
Name: `Hashing and Cryptography`,
Description: `These functions provide basic cryptographic and non-cryptographic functions, ` +
`including cryptographically-secure random number generation.`,
Functions: []funcDef{
{
Name: `murmur3`,
Summary: `Hash the given data using the Murmur3 algorithm.`,
Function: func(input any) uint64 {
return murmur3.Sum64(toBytes(input))
},
}, {
Name: `hash`,
Summary: `Return the hash if the given value using the specified algorithm.`,
Arguments: []funcArg{
{
Name: `cleartext`,
Type: `string`,
Description: `The value to perform a one-way hash operation on.`,
}, {
Name: `algorithm`,
Type: `string`,
Description: `The hash algorithm to use.`,
Valid: []funcArg{
{
Name: `sha1`,
Description: `SHA-1 algorithm`,
}, {
Name: `sha224`,
Description: `SHA-224 algorithm`,
}, {
Name: `sha256`,
Description: `SHA-256 algorithm`,
}, {
Name: `sha384`,
Description: `SHA-384 algorithm`,
}, {
Name: `sha512`,
Description: `SHA-512 algorithm`,
}, {
Name: `md5`,
Description: `MD5 algorithm`,
}, {
Name: `murmur3`,
Description: `Murmur3 algorithm`,
},
},
},
},
Function: func(input any, alg string) (string, error) {
return hashTheThing(alg, input)
},
}, {
Name: `md5`,
Summary: `Return the MD5 hash of the given value.`,
Arguments: []funcArg{
{
Name: `cleartext`,
Type: `string`,
Description: `The value to perform a one-way hash operation on.`,
},
},
Examples: []funcExample{
{
Code: `md5 "p@ssw0rd!"`,
Return: `d5ec75d5fe70d428685510fae36492d9`,
},
},
Function: func(input any) (string, error) {
return hashTheThing(`md5`, input)
},
}, {
Name: `sha1`,
Summary: `Return the SHA-1 hash of the given value.`,
Arguments: []funcArg{
{
Name: `cleartext`,
Type: `string`,
Description: `The value to perform a one-way hash operation on.`,
},
},
Examples: []funcExample{
{
Code: `sha1 "p@ssw0rd!"`,
Return: `ee7161e0fe1a06be63f515302806b34437563c9e`,
},
},
Function: func(input any) (string, error) {
return hashTheThing(`sha1`, input)
},
}, {
Name: `sha224`,
Summary: `Return the SHA-224 hash of the given value.`,
Arguments: []funcArg{
{
Name: `cleartext`,
Type: `string`,
Description: `The value to perform a one-way hash operation on.`,
},
},
Examples: []funcExample{
{
Code: `sha224 "p@ssw0rd!"`,
Return: `2d2e8b944f53164ee0aa8b1f98d75713c1b1bc6b9dd67591ef0a29e0`,
},
},
Function: func(input any) (string, error) {
return hashTheThing(`sha224`, input)
},
}, {
Name: `sha256`,
Summary: `Return the SHA-256 hash of the given value.`,
Arguments: []funcArg{
{
Name: `cleartext`,
Type: `string`,
Description: `The value to perform a one-way hash operation on.`,
},
},
Examples: []funcExample{
{
Code: `sha256 "p@ssw0rd!"`,
Return: `df2191783c6f13274b7c54330a370d0480e82a8a54069b69de73cbfa69f8ea08`,
},
},
Function: func(input any) (string, error) {
return hashTheThing(`sha256`, input)
},
}, {
Name: `sha384`,
Summary: `Return the SHA-384 hash of the given value.`,
Arguments: []funcArg{
{
Name: `cleartext`,
Type: `string`,
Description: `The value to perform a one-way hash operation on.`,
},
},
Examples: []funcExample{
{
Code: `sha384 "p@ssw0rd!"`,
Return: `d6d02abf2b495a6e4350fd985075c88e5a6807f8f79634ddde8529507a6145cb832f40fe0220f2af242a8a4b451fb7fc`,
},
},
Function: func(input any) (string, error) {
return hashTheThing(`sha384`, input)
},
}, {
Name: `sha512`,
Summary: `Return the SHA-512 hash of the given value.`,
Arguments: []funcArg{
{
Name: `cleartext`,
Type: `string`,
Description: `The value to perform a one-way hash operation on.`,
},
},
Examples: []funcExample{
{
Code: `sha512 "p@ssw0rd!"`,
Return: `0f8ea05dd2936700d8f23d7ceb0c7dde03e8dd2dcac714eb465c658412600457ebd143bbf8a00eed47fa0a0677cf2f2ad08f882173546a647c6802ecb19aeeb9`,
},
},
Function: func(input any) (string, error) {
return hashTheThing(`sha512`, input)
},
}, {
Name: `random`,
Summary: `Generates a random number.`,
Arguments: []funcArg{
{
Name: `lower`,
Type: `integer`,
Optional: true,
Description: `If specified, the number generated will be greater than or equal to this number.`,
}, {
Name: `upper`,
Type: `integer`,
Optional: true,
Description: `If specified, the number generated will be strictly less than this number.`,
},
},
Function: func(bounds ...any) int64 {
var min = int64(0)
var max = int64(math.MaxInt64)
switch len(bounds) {
case 2:
max = typeutil.Int(bounds[1])
fallthrough
case 1:
min = typeutil.Int(bounds[0])
}
var delta = (max - min)
return (mrand.Int63n(delta) + min)
},
}, {
Name: `randomBytes`,
Summary: `Return a random array of _n_ bytes. The random source used is ` +
`suitable for cryptographic purposes.`,
Arguments: []funcArg{
{
Name: `count`,
Type: `integer`,
Description: `The size of the output array of random bytes to return.`,
},
},
Function: func(count int) ([]byte, error) {
var output = make([]byte, count)
if _, err := rand.Read(output); err == nil {
return output, nil
} else {
return nil, err
}
},
}, {
Name: `uuid`,
Summary: `Generate a new Version 4 UUID as a string.`,
Function: func() string {
return stringutil.UUID().String()
},
}, {
Name: `uuidRaw`,
Summary: `Generate the raw bytes of a new Version 4 UUID value.`,
Function: func() []byte {
return stringutil.UUID().Bytes()
},
}, {
Name: `hmac`,
Summary: `Generate an HMAC signature for the given input, secret string, and (optionally) hashing algorithm.`,
Arguments: []funcArg{
{
Name: `input`,
Type: `string, bytes`,
Description: `The input to generate an HMAC signature for.`,
}, {
Name: `secret`,
Type: `string`,
Description: `The secret string to use for generating the signature.`,
}, {
Name: `algorithm`,
Type: `string`,
Description: `The name of the hashing algorithm to use for signing.`,
Default: `sha1`,
},
},
Function: func(input any, secret string, alg ...string) (string, error) {
if hasher, err := hashingAlgo(typeutil.OrString(alg, `sha1`)); err == nil {
var hmacca = hmac.New(hasher, []byte(secret))
if _, err := hmacca.Write([]byte(typeutil.String(input))); err == nil {
return hex.EncodeToString(hmacca.Sum(nil)), nil
} else {
return ``, err
}
} else {
return ``, err
}
},
},
},
}
}