github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfsmd/merkle_hash.go (about) 1 // Copyright 2018 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package kbfsmd 6 7 import ( 8 "encoding" 9 10 "github.com/keybase/client/go/kbfs/kbfscodec" 11 "github.com/keybase/client/go/kbfs/kbfshash" 12 ) 13 14 // MerkleHash is the hash of a RootMetadataSigned block. 15 type MerkleHash struct { 16 h kbfshash.Hash 17 } 18 19 // MakeMerkleHash hashes the given signed RootMetadata object. 20 func MakeMerkleHash(codec kbfscodec.Codec, md *RootMetadataSigned) (MerkleHash, error) { 21 buf, err := codec.Encode(md) 22 if err != nil { 23 return MerkleHash{}, err 24 } 25 h, err := kbfshash.DefaultHash(buf) 26 if err != nil { 27 return MerkleHash{}, err 28 } 29 return MerkleHash{h}, nil 30 } 31 32 var _ encoding.BinaryMarshaler = MerkleHash{} 33 var _ encoding.BinaryUnmarshaler = (*MerkleHash)(nil) 34 35 // Bytes returns the bytes of the MerkleHash. 36 func (h MerkleHash) Bytes() []byte { 37 return h.h.Bytes() 38 } 39 40 // String returns the string form of the MerkleHash. 41 func (h MerkleHash) String() string { 42 return h.h.String() 43 } 44 45 // MarshalBinary implements the encoding.BinaryMarshaler interface for 46 // MerkleHash. Returns an error if the MerkleHash is invalid and not the 47 // zero MerkleHash. 48 func (h MerkleHash) MarshalBinary() (data []byte, err error) { 49 return h.h.MarshalBinary() 50 } 51 52 // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface 53 // for MerkleHash. Returns an error if the given byte array is non-empty and 54 // the MerkleHash is invalid. 55 func (h *MerkleHash) UnmarshalBinary(data []byte) error { 56 return h.h.UnmarshalBinary(data) 57 }