2019-02-25 22:44:14 +00:00
|
|
|
package hash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSha256(t *testing.T) {
|
|
|
|
input := []byte("hello")
|
2019-08-23 14:08:19 +00:00
|
|
|
data := Sha256(input)
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
expected := "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
|
2019-08-23 14:02:42 +00:00
|
|
|
actual := hex.EncodeToString(data.Bytes())
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashDoubleSha256(t *testing.T) {
|
|
|
|
input := []byte("hello")
|
2019-08-23 14:08:19 +00:00
|
|
|
data := DoubleSha256(input)
|
2019-02-25 22:44:14 +00:00
|
|
|
|
2019-08-23 14:08:19 +00:00
|
|
|
firstSha := Sha256(input)
|
|
|
|
doubleSha := Sha256(firstSha.Bytes())
|
2019-02-25 22:44:14 +00:00
|
|
|
expected := hex.EncodeToString(doubleSha.Bytes())
|
|
|
|
|
|
|
|
actual := hex.EncodeToString(data.Bytes())
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashRipeMD160(t *testing.T) {
|
|
|
|
input := []byte("hello")
|
|
|
|
data, err := RipeMD160(input)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expected := "108f07b8382412612c048d07d13f814118445acd"
|
|
|
|
actual := hex.EncodeToString(data.Bytes())
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHash160(t *testing.T) {
|
|
|
|
input := "02cccafb41b220cab63fd77108d2d1ebcffa32be26da29a04dca4996afce5f75db"
|
|
|
|
publicKeyBytes, _ := hex.DecodeString(input)
|
|
|
|
data, err := Hash160(publicKeyBytes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expected := "c8e2b685cc70ec96743b55beb9449782f8f775d8"
|
|
|
|
actual := hex.EncodeToString(data.Bytes())
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|