This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
/
hash.go
103 lines (91 loc) · 2.41 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
102
103
package bc
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"golang.org/x/crypto/sha3"
)
// EmptyStringHash represents a 256-bit hash.
var EmptyStringHash = NewHash(sha3.Sum256(nil))
// NewHash convert the input byte array to hash
func NewHash(b32 [32]byte) (h Hash) {
h.V0 = binary.BigEndian.Uint64(b32[0:8])
h.V1 = binary.BigEndian.Uint64(b32[8:16])
h.V2 = binary.BigEndian.Uint64(b32[16:24])
h.V3 = binary.BigEndian.Uint64(b32[24:32])
return h
}
// Byte32 return the byte array representation
func (h Hash) Byte32() (b32 [32]byte) {
binary.BigEndian.PutUint64(b32[0:8], h.V0)
binary.BigEndian.PutUint64(b32[8:16], h.V1)
binary.BigEndian.PutUint64(b32[16:24], h.V2)
binary.BigEndian.PutUint64(b32[24:32], h.V3)
return b32
}
// MarshalText satisfies the TextMarshaler interface.
// It returns the bytes of h encoded in hex,
// for formats that can't hold arbitrary binary data.
// It never returns an error.
func (h Hash) MarshalText() ([]byte, error) {
b := h.Byte32()
v := make([]byte, 64)
hex.Encode(v, b[:])
return v, nil
}
// UnmarshalText satisfies the TextUnmarshaler interface.
// It decodes hex data from b into h.
func (h *Hash) UnmarshalText(v []byte) error {
var b [32]byte
if len(v) != 64 {
return fmt.Errorf("bad length hash string %d", len(v))
}
_, err := hex.Decode(b[:], v)
*h = NewHash(b)
return err
}
// UnmarshalJSON satisfies the json.Unmarshaler interface.
// If b is a JSON-encoded null, it copies the zero-value into h. Othwerwise, it
// decodes hex data from b into h.
func (h *Hash) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
*h = Hash{}
return nil
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return h.UnmarshalText([]byte(s))
}
// Bytes returns the byte representation
func (h Hash) Bytes() []byte {
b32 := h.Byte32()
return b32[:]
}
// WriteTo satisfies the io.WriterTo interface.
func (h Hash) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(h.Bytes())
return int64(n), err
}
// ReadFrom satisfies the io.ReaderFrom interface.
func (h *Hash) ReadFrom(r io.Reader) (int64, error) {
var b32 [32]byte
n, err := io.ReadFull(r, b32[:])
if err != nil {
return int64(n), err
}
*h = NewHash(b32)
return int64(n), nil
}
// IsZero tells whether a Hash pointer is nil or points to an all-zero
// hash.
func (h *Hash) IsZero() bool {
if h == nil {
return true
}
return *h == Hash{}
}