Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions store/internal/tree/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tree

import (
"crypto/sha256"
"encoding/hex"
"hash"
"math/bits"
)
Expand All @@ -17,10 +18,22 @@ func HashFromByteSlices(items [][]byte) []byte {
return hashFromByteSlices(sha256.New(), items)
}

// generate from this code
//
// func emptyHash() []byte {
// h := sha256.Sum256([]byte{})
// return h[:]
// }
//
// func main() {
// println(hex.EncodeToString(emptyHash()))
// }
var emptyHash, _ = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")

func hashFromByteSlices(sha hash.Hash, items [][]byte) []byte {
switch len(items) {
case 0:
return emptyHash()
return emptyHash
case 1:
return leafHashOpt(sha, items[0])
default:
Expand All @@ -47,12 +60,6 @@ func innerHashOpt(s hash.Hash, left, right []byte) []byte {
return s.Sum(nil)
}

// emptyHash returns tmhash(<empty>)
func emptyHash() []byte {
h := sha256.Sum256([]byte{})
return h[:]
}

// getSplitPoint returns the largest power of 2 less than length
func getSplitPoint(length int64) int64 {
if length < 1 {
Expand Down
Loading