-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo.go
120 lines (103 loc) · 2.33 KB
/
algo.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
package gocrypto
import (
"crypto"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"golang.org/x/crypto/sha3"
)
// HashingAlgo represents the hashing algorithm.
type HashingAlgo string
const (
MD5 HashingAlgo = "MD5" // MD5 algorithm (not recommended for security)
SHA1 HashingAlgo = "SHA1" // SHA1 algorithm (not recommended for security)
SHA224 HashingAlgo = "SHA224" // SHA2 224 algorithm
SHA256 HashingAlgo = "SHA256" // SHA2 256 algorithm (recommended)
SHA384 HashingAlgo = "SHA384" // SHA2 384 algorithm
SHA512 HashingAlgo = "SHA512" // SHA2 512 algorithm
SHA3224 HashingAlgo = "SHA3224" // SHA3 224 algorithm
SHA3256 HashingAlgo = "SHA3256" // SHA3 256 algorithm
SHA3384 HashingAlgo = "SHA3384" // SHA3 384 algorithm
SHA3512 HashingAlgo = "SHA3512" // SHA3 512 algorithm
)
// HashingInstance returns hashing instance for algo.
// It returns nil if algo is not supported.
func HashingInstance(algo HashingAlgo) func() hash.Hash {
switch algo {
case MD5:
return md5.New
case SHA1:
return sha1.New
case SHA224:
return sha256.New224
case SHA256:
return sha256.New
case SHA384:
return sha512.New384
case SHA512:
return sha512.New
case SHA3224:
return sha3.New224
case SHA3256:
return sha3.New256
case SHA3384:
return sha3.New384
case SHA3512:
return sha3.New512
}
return nil
}
// HashingSize returns the size of the hash for the algo.
func HashingSize(algo HashingAlgo) int {
switch algo {
case MD5:
return md5.Size
case SHA1:
return sha1.Size
case SHA224:
return sha256.Size224
case SHA256:
return sha256.Size
case SHA384:
return sha512.Size384
case SHA512:
return sha512.Size
case SHA3224:
return sha3.New224().Size()
case SHA3256:
return sha3.New256().Size()
case SHA3384:
return sha3.New384().Size()
case SHA3512:
return sha3.New512().Size()
}
return 0
}
// HashingAlg returns the crypto.Hash for the algo.
func HashingAlg(algo HashingAlgo) crypto.Hash {
switch algo {
case MD5:
return crypto.MD5
case SHA1:
return crypto.SHA1
case SHA224:
return crypto.SHA224
case SHA256:
return crypto.SHA256
case SHA384:
return crypto.SHA384
case SHA512:
return crypto.SHA512
case SHA3224:
return crypto.SHA3_224
case SHA3256:
return crypto.SHA3_256
case SHA3384:
return crypto.SHA3_384
case SHA3512:
return crypto.SHA3_512
}
return 0
}