-
Notifications
You must be signed in to change notification settings - Fork 3
/
hash.go
46 lines (39 loc) · 782 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
package simpleBlockchain
import (
"encoding/hex"
"encoding/json"
)
type Hashes []byte
func (h Hashes) MarshalJSON() ([]byte, error) {
res, err:= json.Marshal(hex.EncodeToString(h))
if err != nil {
return nil, err
}
return res, nil
}
func (h *Hashes) UnmarshalText(text []byte) (err error) {
b, err := hex.DecodeString(string(text))
if err != nil {
return err
}
*h = b
return
}
func (h Hashes) String() string {
bs, _ := json.MarshalIndent(h,""," ")
return string(bs) + "\n"
}
func HashestoBytes(h []Hashes) ([][]byte) {
ans := make([][]byte, len(h))
for i, hash := range h {
ans[i] = hash
}
return ans
}
func bytesToHashes(inputs [][]byte) []Hashes{
ans := make([]Hashes, len(inputs))
for i, input := range inputs {
ans[i] = input
}
return ans
}