-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
merkle_hash.go
57 lines (47 loc) · 1.54 KB
/
merkle_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
// Copyright 2018 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfsmd
import (
"encoding"
"github.com/keybase/client/go/kbfs/kbfscodec"
"github.com/keybase/client/go/kbfs/kbfshash"
)
// MerkleHash is the hash of a RootMetadataSigned block.
type MerkleHash struct {
h kbfshash.Hash
}
// MakeMerkleHash hashes the given signed RootMetadata object.
func MakeMerkleHash(codec kbfscodec.Codec, md *RootMetadataSigned) (MerkleHash, error) {
buf, err := codec.Encode(md)
if err != nil {
return MerkleHash{}, err
}
h, err := kbfshash.DefaultHash(buf)
if err != nil {
return MerkleHash{}, err
}
return MerkleHash{h}, nil
}
var _ encoding.BinaryMarshaler = MerkleHash{}
var _ encoding.BinaryUnmarshaler = (*MerkleHash)(nil)
// Bytes returns the bytes of the MerkleHash.
func (h MerkleHash) Bytes() []byte {
return h.h.Bytes()
}
// String returns the string form of the MerkleHash.
func (h MerkleHash) String() string {
return h.h.String()
}
// MarshalBinary implements the encoding.BinaryMarshaler interface for
// MerkleHash. Returns an error if the MerkleHash is invalid and not the
// zero MerkleHash.
func (h MerkleHash) MarshalBinary() (data []byte, err error) {
return h.h.MarshalBinary()
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
// for MerkleHash. Returns an error if the given byte array is non-empty and
// the MerkleHash is invalid.
func (h *MerkleHash) UnmarshalBinary(data []byte) error {
return h.h.UnmarshalBinary(data)
}