-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
58 lines (46 loc) · 931 Bytes
/
utils.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
package srp6ago
import (
"crypto"
"encoding/hex"
"fmt"
"regexp"
)
var trimRE = regexp.MustCompile(`\s+|\r+\n+`)
func Hex2Bytes(input string) ([]byte, error) {
return hex.DecodeString(trimRE.ReplaceAllLiteralString(input, ""))
}
func MustHex2Bytes(input string) []byte {
out, err := Hex2Bytes(input)
if err != nil {
panic(fmt.Errorf("failed to decode a hex: %w", err))
}
return out
}
func pad(size int, input []byte) []byte {
if len(input) >= size {
return input
}
diff := size - len(input)
out := make([]byte, diff+len(input))
copy(out[diff:], input)
return out
}
func hash(name string, inputs ...[]byte) []byte {
h := newHash(name).New()
for ix := range inputs {
h.Write(inputs[ix])
}
return h.Sum(nil)
}
func newHash(enum string) crypto.Hash {
switch enum {
case Sha1:
return crypto.SHA1
case Sha256:
return crypto.SHA256
case Sha512:
return crypto.SHA512
default:
return 0
}
}