-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure_hash.go
74 lines (57 loc) · 1.22 KB
/
secure_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
package object
import (
"crypto/sha512"
"fmt"
"hash"
"lukechampine.com/blake3"
)
const blake3Size = 64 // 512 bits
type secureHash struct {
Blake3 *blake3.Hasher
SHA512 hash.Hash
}
var _ hash.Hash = (*secureHash)(nil)
func newSecureHash() *secureHash {
return &secureHash{
Blake3: blake3.New(blake3Size, nil),
SHA512: sha512.New(),
}
}
func (h *secureHash) Write(
p []byte,
) (n int, err error) {
n0, err := h.Blake3.Write(p)
if err != nil {
return n0, fmt.Errorf("unable to update the Blake3 part of the hash: %w", err)
}
n1, err := h.SHA512.Write(p)
if err != nil {
return max(n0, n1), fmt.Errorf("unable to update the SHA512 part of the hash: %w", err)
}
return max(n0, n1), nil
}
func (h *secureHash) Sum(
b []byte,
) []byte {
result := make([]byte, len(b)+h.Size())
idx := 0
copy(result[idx:], b)
idx += len(b)
h0 := h.Blake3.Sum(nil)
copy(result[idx:], h0)
idx += len(h0)
h1 := h.SHA512.Sum(nil)
copy(result[idx:], h1)
idx += len(h1)
return result
}
func (h *secureHash) Reset() {
h.Blake3.Reset()
h.SHA512.Reset()
}
func (h *secureHash) Size() int {
return h.Blake3.Size() + h.SHA512.Size()
}
func (h *secureHash) BlockSize() int {
return max(h.Blake3.BlockSize(), h.SHA512.BlockSize())
}