-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.go
101 lines (90 loc) · 1.78 KB
/
hash.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
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package altcha
import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"hash"
"strconv"
"sync"
)
type Algorithm int
const (
UnknownAlgorithm Algorithm = iota
SHA256
SHA384
SHA512
)
func (algorithm Algorithm) String() string {
switch algorithm {
case SHA256:
return "SHA-256"
case SHA384:
return "SHA-384"
case SHA512:
return "SHA-512"
default:
panic("unknown hashing algorithm")
}
}
func AlgorithmFromString(algo string) (Algorithm, bool) {
switch algo {
case "SHA-256":
return SHA256, true
case "SHA-384":
return SHA384, true
case "SHA-512":
return SHA512, true
default:
return UnknownAlgorithm, false
}
}
var hasherPoolSha256 = sync.Pool{
New: func() interface{} {
return sha256.New()
},
}
var hasherPoolSha384 = sync.Pool{
New: func() interface{} {
return sha512.New384()
},
}
var hasherPoolSha512 = sync.Pool{
New: func() interface{} {
return sha512.New()
},
}
func getHasher(algo Algorithm) (hasher hash.Hash, put func()) {
switch algo {
case SHA256:
hasher = hasherPoolSha256.Get().(hash.Hash)
put = func() {
hasher.Reset()
hasherPoolSha256.Put(hasher)
}
case SHA384:
hasher = hasherPoolSha384.Get().(hash.Hash)
put = func() {
hasher.Reset()
hasherPoolSha384.Put(hasher)
}
case SHA512:
hasher = hasherPoolSha512.Get().(hash.Hash)
put = func() {
hasher.Reset()
hasherPoolSha512.Put(hasher)
}
default:
panic("unknown hashing algorithm")
}
return hasher, put
}
func generateHash(algo Algorithm, salt string, number int) string {
hasher, put := getHasher(algo)
defer put()
hasher.Write([]byte(salt))
hasher.Write([]byte(strconv.Itoa(number)))
return hex.EncodeToString(hasher.Sum(nil))
}