Browse Source

tmhash

pull/1780/head
Ethan Buchman 7 years ago
parent
commit
d82699bf43
4 changed files with 69 additions and 5 deletions
  1. +2
    -2
      merkle/simple_map.go
  2. +3
    -3
      merkle/simple_tree.go
  3. +41
    -0
      merkle/tmhash/hash.go
  4. +23
    -0
      merkle/tmhash/hash_test.go

+ 2
- 2
merkle/simple_map.go View File

@ -2,7 +2,7 @@ package merkle
import (
cmn "github.com/tendermint/tmlibs/common"
"golang.org/x/crypto/ripemd160"
"github.com/tendermint/tmlibs/merkle/tmhash"
)
type SimpleMap struct {
@ -63,7 +63,7 @@ func (sm *SimpleMap) KVPairs() cmn.KVPairs {
type KVPair cmn.KVPair
func (kv KVPair) Hash() []byte {
hasher := ripemd160.New()
hasher := tmhash.New()
err := encodeByteSlice(hasher, kv.Key)
if err != nil {
panic(err)


+ 3
- 3
merkle/simple_tree.go View File

@ -25,11 +25,11 @@ For larger datasets, use IAVLTree.
package merkle
import (
"golang.org/x/crypto/ripemd160"
"github.com/tendermint/tmlibs/merkle/tmhash"
)
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte {
var hasher = ripemd160.New()
var hasher = tmhash.New()
err := encodeByteSlice(hasher, left)
if err != nil {
panic(err)
@ -68,7 +68,7 @@ func SimpleHashFromByteslices(bzs [][]byte) []byte {
}
func SimpleHashFromBytes(bz []byte) []byte {
hasher := ripemd160.New()
hasher := tmhash.New()
hasher.Write(bz)
return hasher.Sum(nil)
}


+ 41
- 0
merkle/tmhash/hash.go View File

@ -0,0 +1,41 @@
package tmhash
import (
"crypto/sha256"
"hash"
)
var (
Size = 20
BlockSize = sha256.BlockSize
)
type sha256trunc struct {
sha256 hash.Hash
}
func (h sha256trunc) Write(p []byte) (n int, err error) {
return h.sha256.Write(p)
}
func (h sha256trunc) Sum(b []byte) []byte {
shasum := h.sha256.Sum(b)
return shasum[:Size]
}
func (h sha256trunc) Reset() {
h.sha256.Reset()
}
func (h sha256trunc) Size() int {
return Size
}
func (h sha256trunc) BlockSize() int {
return h.sha256.BlockSize()
}
func New() hash.Hash {
return sha256trunc{
sha256: sha256.New(),
}
}

+ 23
- 0
merkle/tmhash/hash_test.go View File

@ -0,0 +1,23 @@
package tmhash_test
import (
"crypto/sha256"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tmlibs/merkle/tmhash"
)
func TestHash(t *testing.T) {
testVector := []byte("abc")
hasher := tmhash.New()
hasher.Write(testVector)
bz := hasher.Sum(nil)
hasher = sha256.New()
hasher.Write(testVector)
bz2 := hasher.Sum(nil)
bz2 = bz2[:20]
assert.Equal(t, bz, bz2)
}

Loading…
Cancel
Save