forked from celestiaorg/go-header
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
53 lines (46 loc) · 1.15 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
package header
import (
"encoding/hex"
"fmt"
)
// Hash represents cryptographic hash and provides basic serialization functions.
type Hash []byte
// String implements fmt.Stringer interface.
func (h Hash) String() string {
buf := make([]byte, hex.EncodedLen(len(h)))
hex.Encode(buf, h)
hexToUpper(buf)
return string(buf)
}
// MarshalJSON serializes Hash into valid JSON.
func (h Hash) MarshalJSON() ([]byte, error) {
buf := make([]byte, 2+hex.EncodedLen(len(h)))
buf[0] = '"'
hex.Encode(buf[1:], h)
hexToUpper(buf)
buf[len(buf)-1] = '"'
return buf, nil
}
// UnmarshalJSON deserializes JSON representation of a Hash into object.
func (h *Hash) UnmarshalJSON(data []byte) error {
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return fmt.Errorf("invalid hex string: %s", data)
}
buf := make([]byte, hex.DecodedLen(len(data)-2))
_, err := hex.Decode(buf, data[1:len(data)-1])
if err != nil {
return err
}
*h = buf
return nil
}
// because we encode hex (alphabet: 0-9a-f) we can do this inplace.
func hexToUpper(b []byte) {
for i := 0; i < len(b); i++ {
c := b[i]
if 'a' <= c && c <= 'z' {
c -= 'a' - 'A'
}
b[i] = c
}
}