neoneo-go/pkg/crypto/hash/hash_test.go

67 lines
1.5 KiB
Go
Raw Normal View History

2019-02-25 22:44:14 +00:00
package hash
import (
2019-11-24 15:06:00 +00:00
"encoding/binary"
2019-02-25 22:44:14 +00:00
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
2019-11-24 15:06:00 +00:00
"github.com/stretchr/testify/require"
2019-02-25 22:44:14 +00:00
)
func TestSha256(t *testing.T) {
input := []byte("hello")
data := Sha256(input)
2019-02-25 22:44:14 +00:00
expected := "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
2019-11-27 09:23:18 +00:00
actual := hex.EncodeToString(data.BytesBE())
2019-02-25 22:44:14 +00:00
assert.Equal(t, expected, actual)
}
func TestHashDoubleSha256(t *testing.T) {
input := []byte("hello")
data := DoubleSha256(input)
2019-02-25 22:44:14 +00:00
firstSha := Sha256(input)
2019-11-27 09:23:18 +00:00
doubleSha := Sha256(firstSha.BytesBE())
expected := hex.EncodeToString(doubleSha.BytesBE())
2019-02-25 22:44:14 +00:00
2019-11-27 09:23:18 +00:00
actual := hex.EncodeToString(data.BytesBE())
2019-02-25 22:44:14 +00:00
assert.Equal(t, expected, actual)
}
func TestHashRipeMD160(t *testing.T) {
input := []byte("hello")
data := RipeMD160(input)
2019-02-25 22:44:14 +00:00
expected := "108f07b8382412612c048d07d13f814118445acd"
2019-11-27 09:20:31 +00:00
actual := hex.EncodeToString(data.BytesBE())
2019-02-25 22:44:14 +00:00
assert.Equal(t, expected, actual)
}
func TestHash160(t *testing.T) {
input := "02cccafb41b220cab63fd77108d2d1ebcffa32be26da29a04dca4996afce5f75db"
publicKeyBytes, _ := hex.DecodeString(input)
data := Hash160(publicKeyBytes)
2019-02-25 22:44:14 +00:00
expected := "c8e2b685cc70ec96743b55beb9449782f8f775d8"
2019-11-27 09:20:31 +00:00
actual := hex.EncodeToString(data.BytesBE())
2019-02-25 22:44:14 +00:00
assert.Equal(t, expected, actual)
}
2019-11-24 15:06:00 +00:00
func TestChecksum(t *testing.T) {
testCases := []struct {
data []byte
sum uint32
}{
{nil, 0xe2e0f65d},
{[]byte{}, 0xe2e0f65d},
{[]byte{1, 2, 3, 4}, 0xe272e48d},
}
for _, tc := range testCases {
require.Equal(t, tc.sum, binary.LittleEndian.Uint32(Checksum(tc.data)))
}
}