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.
This commit is contained in:
Roman Khimov 2019-08-23 17:23:11 +03:00
parent 80c3c9035a
commit 3fa6ba9c7b
2 changed files with 11 additions and 26 deletions

View file

@ -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

View file

@ -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)