mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-05 11:31:16 +00:00
tmhash
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
merkle/tmhash/hash.go
Normal file
41
merkle/tmhash/hash.go
Normal 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
merkle/tmhash/hash_test.go
Normal file
23
merkle/tmhash/hash_test.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user