forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
147 lines (116 loc) · 2.98 KB
/
hash.go
File metadata and controls
147 lines (116 loc) · 2.98 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package notaryapi
import (
"bytes"
"fmt"
"crypto/sha256"
"github.com/FactomProject/gocoding"
"reflect"
"encoding/hex"
)
// Size of array used to store sha hashes. See ShaHash.
const HashSize = 32
type Hash struct {
Bytes []byte `json:"bytes"`
}
func EmptyHash() (h *Hash) {
h = new(Hash)
h.Bytes = make([]byte, 32)
return
}
func CreateHash(entities...BinaryMarshallable) (h *Hash, err error) {
sha := sha256.New()
for _, entity := range entities {
data, err := entity.MarshalBinary()
if err != nil { return nil, err }
sha.Write(data)
}
h = new(Hash)
h.Bytes = sha.Sum(nil)
return
}
func (h *Hash) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
buf.Write([]byte{byte(len(h.Bytes))})
buf.Write(h.Bytes)
return buf.Bytes(), nil
}
func (h *Hash) MarshalledSize() uint64 {
return uint64(len(h.Bytes)) + 1
}
func (h *Hash) UnmarshalBinary(data []byte) error {
h.Bytes = make([]byte, data[0])
if data[0] > byte(0){
data = data[1:]
copy(h.Bytes, data)
}
return nil
}
func (h *Hash) Encoding(marshaller gocoding.Marshaller, theType reflect.Type) gocoding.Encoder {
return func(scratch [64]byte, renderer gocoding.Renderer, value reflect.Value) {
hash := value.Interface().(*Hash)
marshaller.MarshalObject(renderer, hash.Bytes)
}
}
func (h *Hash) Decoding(unmarshaller gocoding.Unmarshaller, theType reflect.Type) gocoding.Decoder {
return func(scratch [64]byte, scanner gocoding.Scanner, value reflect.Value) {
if value.IsNil() {
value.Set(reflect.ValueOf(new(Hash)))
}
hash := value.Interface().(*Hash)
unmarshaller.UnmarshalObject(scanner, &hash.Bytes)
}
}
func (hash *Hash) GetBytes() []byte {
newHash := make([]byte, HashSize)
copy(newHash, hash.Bytes)
return newHash
}
// SetBytes sets the bytes which represent the hash. An error is returned if
// the number of bytes passed in is not HashSize.
func (hash *Hash) SetBytes(newHash []byte) error {
nhlen := len(newHash)
if nhlen != HashSize {
return fmt.Errorf("invalid sha length of %v, want %v", nhlen,
HashSize)
}
hash.Bytes = make([]byte, HashSize)
copy(hash.Bytes, newHash)
return nil
}
// NewShaHash returns a new ShaHash from a byte slice. An error is returned if
// the number of bytes passed in is not HashSize.
func NewHash(newHash []byte) (*Hash, error) {
var sh Hash
err := sh.SetBytes(newHash)
if err != nil {
return nil, err
}
return &sh, err
}
func Sha(data []byte) (h *Hash) {
sha := sha256.New()
sha.Write(data)
h = new(Hash)
h.Bytes = sha.Sum(nil)
return h
}
func (h *Hash) String() string {
return hex.EncodeToString(h.Bytes)
}
func (h *Hash) ByteString() string {
return string(h.Bytes)
}
func HexToHash(hexStr string) (h *Hash, err error) {
h = new (Hash)
h.Bytes, err = hex.DecodeString(hexStr)
return h, err
}
// String returns the ShaHash in the standard bitcoin big-endian form.
func (h *Hash) BTCString() string {
hashstr := ""
hash := h.Bytes
for i := range hash {
hashstr += fmt.Sprintf("%02x", hash[HashSize-1-i])
}
return hashstr
}