From 80c3c9035a3ac1f463637615d87b90b83ae58931 Mon Sep 17 00:00:00 2001
From: Roman Khimov <roman@nspcc.ru>
Date: Fri, 23 Aug 2019 17:08:19 +0300
Subject: [PATCH] hash: simplify Sha256 implementations

Use sha256.Sum256() and drop duplicating Sum() completely.
---
 pkg/crypto/hash/hash.go      | 47 +++++++++---------------------------
 pkg/crypto/hash/hash_test.go | 15 +++---------
 2 files changed, 15 insertions(+), 47 deletions(-)

diff --git a/pkg/crypto/hash/hash.go b/pkg/crypto/hash/hash.go
index 3b9197484..aff161a83 100755
--- a/pkg/crypto/hash/hash.go
+++ b/pkg/crypto/hash/hash.go
@@ -10,33 +10,18 @@ import (
 
 // Sha256 hashes the incoming byte slice
 // using the sha256 algorithm
-func Sha256(data []byte) (util.Uint256, error) {
-	var hash util.Uint256
-	hasher := sha256.New()
-	hasher.Reset()
-	_, err := hasher.Write(data)
-
-	hash, err = util.Uint256DecodeBytes(hasher.Sum(nil))
-	if err != nil {
-		return hash, err
-	}
-	return hash, nil
+func Sha256(data []byte) util.Uint256 {
+	hash := sha256.Sum256(data)
+	return hash
 }
 
 // DoubleSha256 performs sha256 twice on the given data
-func DoubleSha256(data []byte) (util.Uint256, error) {
+func DoubleSha256(data []byte) util.Uint256 {
 	var hash util.Uint256
 
-	h1, err := Sha256(data)
-	if err != nil {
-		return hash, err
-	}
-
-	hash, err = Sha256(h1.Bytes())
-	if err != nil {
-		return hash, err
-	}
-	return hash, nil
+	h1 := Sha256(data)
+	hash = Sha256(h1.Bytes())
+	return hash
 }
 
 // RipeMD160 performs the RIPEMD160 hash algorithm
@@ -58,7 +43,7 @@ func RipeMD160(data []byte) (util.Uint160, error) {
 // on the given data
 func Hash160(data []byte) (util.Uint160, error) {
 	var hash util.Uint160
-	h1, err := Sha256(data)
+	h1 := Sha256(data)
 
 	h2, err := RipeMD160(h1.Bytes())
 
@@ -72,17 +57,7 @@ func Hash160(data []byte) (util.Uint160, error) {
 
 // Checksum returns the checksum for a given piece of data
 // using sha256 twice as the hash algorithm
-func Checksum(data []byte) ([]byte, error) {
-	hash, err := Sum(data)
-	if err != nil {
-		return nil, err
-	}
-	return hash[:4], nil
-}
-
-// Sum performs sha256 twice on the given data
-// XXX(issue): We should remove this and just do doublesha256
-func Sum(b []byte) (util.Uint256, error) {
-	hash, err := DoubleSha256((b))
-	return hash, err
+func Checksum(data []byte) []byte {
+	hash := DoubleSha256(data)
+	return hash[:4]
 }
diff --git a/pkg/crypto/hash/hash_test.go b/pkg/crypto/hash/hash_test.go
index b0ca0633f..8e5723823 100755
--- a/pkg/crypto/hash/hash_test.go
+++ b/pkg/crypto/hash/hash_test.go
@@ -9,11 +9,8 @@ import (
 
 func TestSha256(t *testing.T) {
 	input := []byte("hello")
-	data, err := Sha256(input)
+	data := Sha256(input)
 
-	if err != nil {
-		t.Fatal(err)
-	}
 	expected := "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
 	actual := hex.EncodeToString(data.Bytes())
 
@@ -22,14 +19,10 @@ func TestSha256(t *testing.T) {
 
 func TestHashDoubleSha256(t *testing.T) {
 	input := []byte("hello")
-	data, err := DoubleSha256(input)
+	data := DoubleSha256(input)
 
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	firstSha, _ := Sha256(input)
-	doubleSha, _ := Sha256(firstSha.Bytes())
+	firstSha := Sha256(input)
+	doubleSha := Sha256(firstSha.Bytes())
 	expected := hex.EncodeToString(doubleSha.Bytes())
 
 	actual := hex.EncodeToString(data.Bytes())