-
Notifications
You must be signed in to change notification settings - Fork 7
/
hash.go
executable file
·56 lines (46 loc) · 991 Bytes
/
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
package fields
import (
"bytes"
"github.com/hacash/core/crypto/sha3"
)
type Hash = Bytes32
type HashHalfChecker = Bytes16
type HashNonceChecker = Bytes8
const (
HashSize = 32
HashHalfCheckerSize = 16
HashNonceCheckerSize = 8
)
// Sha3 calculate 32-bit hash value
func CalculateHash(stuff []byte) Hash {
hx := sha3.Sum256(stuff)
return hx[:]
}
func (h Hash) GetHalfChecker() HashHalfChecker {
half := make([]byte, 16)
copy(half, h)
return half
}
func (h Hash) GetNonceChecker() HashNonceChecker {
nonce := make([]byte, 8)
copy(nonce, h)
return nonce
}
func (h Hash) Equal(tar Hash) bool {
return bytes.Compare(h, tar) == 0
}
func (h Hash) NotZeroBlank() bool {
for i := len(h) - 1; i >= 0; i-- {
if h[i] > 0 {
return true
}
}
// empty
return false
}
func (h HashHalfChecker) Equal(tar HashHalfChecker) bool {
return bytes.Compare(h, tar) == 0
}
func (h HashNonceChecker) Equal(tar HashNonceChecker) bool {
return bytes.Compare(h, tar) == 0
}