forked from celestiaorg/go-header
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
38 lines (33 loc) · 896 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
package header
import (
"encoding/hex"
"fmt"
"strings"
)
// Hash represents cryptographic hash and provides basic serialization functions.
type Hash []byte
// String implements fmt.Stringer interface.
func (h Hash) String() string {
return strings.ToUpper(hex.EncodeToString(h))
}
// MarshalJSON serializes Hash into valid JSON.
func (h Hash) MarshalJSON() ([]byte, error) {
s := strings.ToUpper(hex.EncodeToString(h))
jbz := make([]byte, len(s)+2)
jbz[0] = '"'
copy(jbz[1:], s)
jbz[len(jbz)-1] = '"'
return jbz, 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)
}
bz2, err := hex.DecodeString(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*h = bz2
return nil
}