From 3fa6ba9c7b7f69961ffdf3cc042e46101655dac2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 23 Aug 2019 17:23:11 +0300 Subject: [PATCH] hash: simplify ripemd160 usage Go's Hash is explicitly specified to never return an error on Write(), and our own decoding functions only check for length which is gonna be right in every case so it makes no sense returning errors from these functions. --- pkg/crypto/hash/hash.go | 27 +++++++++------------------ pkg/crypto/hash/hash_test.go | 10 ++-------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/pkg/crypto/hash/hash.go b/pkg/crypto/hash/hash.go index aff161a83..ba3c6783b 100755 --- a/pkg/crypto/hash/hash.go +++ b/pkg/crypto/hash/hash.go @@ -2,7 +2,6 @@ package hash import ( "crypto/sha256" - "io" "github.com/CityOfZion/neo-go/pkg/util" "golang.org/x/crypto/ripemd160" @@ -26,33 +25,25 @@ func DoubleSha256(data []byte) util.Uint256 { // RipeMD160 performs the RIPEMD160 hash algorithm // on the given data -func RipeMD160(data []byte) (util.Uint160, error) { +func RipeMD160(data []byte) util.Uint160 { var hash util.Uint160 hasher := ripemd160.New() - hasher.Reset() - _, err := io.WriteString(hasher, string(data)) + _, _ = hasher.Write(data) - hash, err = util.Uint160DecodeBytes(hasher.Sum(nil)) - if err != nil { - return hash, err - } - return hash, nil + hash, _ = util.Uint160DecodeBytes(hasher.Sum(nil)) + return hash } // Hash160 performs sha256 and then ripemd160 // on the given data -func Hash160(data []byte) (util.Uint160, error) { +func Hash160(data []byte) util.Uint160 { var hash util.Uint160 + h1 := Sha256(data) + h2 := RipeMD160(h1.Bytes()) + hash, _ = util.Uint160DecodeBytes(h2.Bytes()) - h2, err := RipeMD160(h1.Bytes()) - - hash, err = util.Uint160DecodeBytes(h2.Bytes()) - - if err != nil { - return hash, err - } - return hash, nil + return hash } // Checksum returns the checksum for a given piece of data diff --git a/pkg/crypto/hash/hash_test.go b/pkg/crypto/hash/hash_test.go index 8e5723823..272b744fd 100755 --- a/pkg/crypto/hash/hash_test.go +++ b/pkg/crypto/hash/hash_test.go @@ -31,11 +31,8 @@ func TestHashDoubleSha256(t *testing.T) { func TestHashRipeMD160(t *testing.T) { input := []byte("hello") - data, err := RipeMD160(input) + data := RipeMD160(input) - if err != nil { - t.Fatal(err) - } expected := "108f07b8382412612c048d07d13f814118445acd" actual := hex.EncodeToString(data.Bytes()) assert.Equal(t, expected, actual) @@ -44,11 +41,8 @@ func TestHashRipeMD160(t *testing.T) { func TestHash160(t *testing.T) { input := "02cccafb41b220cab63fd77108d2d1ebcffa32be26da29a04dca4996afce5f75db" publicKeyBytes, _ := hex.DecodeString(input) - data, err := Hash160(publicKeyBytes) + data := Hash160(publicKeyBytes) - if err != nil { - t.Fatal(err) - } expected := "c8e2b685cc70ec96743b55beb9449782f8f775d8" actual := hex.EncodeToString(data.Bytes()) assert.Equal(t, expected, actual)