2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-08-27 14:06:32 +00:00
|
|
|
func TestPubKeyVerify(t *testing.T) {
|
|
|
|
var data = []byte("sample")
|
2019-08-27 13:29:42 +00:00
|
|
|
hashedData := hash.Sha256(data)
|
|
|
|
|
|
|
|
privKey, err := NewPrivateKey()
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Nil(t, err)
|
2019-08-27 13:29:42 +00:00
|
|
|
signedData, err := privKey.Sign(data)
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Nil(t, err)
|
2019-09-05 06:35:02 +00:00
|
|
|
pubKey := privKey.PublicKey()
|
2019-11-27 09:23:18 +00:00
|
|
|
result := pubKey.Verify(signedData, hashedData.BytesBE())
|
2019-08-27 13:29:42 +00:00
|
|
|
expected := true
|
2019-08-27 14:06:32 +00:00
|
|
|
assert.Equal(t, expected, result)
|
2019-11-24 15:40:07 +00:00
|
|
|
|
|
|
|
pubKey = &PublicKey{}
|
2019-11-27 09:23:18 +00:00
|
|
|
assert.False(t, pubKey.Verify(signedData, hashedData.BytesBE()))
|
2019-08-27 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrongPubKey(t *testing.T) {
|
|
|
|
privKey, _ := NewPrivateKey()
|
|
|
|
sample := []byte("sample")
|
|
|
|
hashedData := hash.Sha256(sample)
|
|
|
|
signedData, _ := privKey.Sign(sample)
|
|
|
|
|
|
|
|
secondPrivKey, _ := NewPrivateKey()
|
2019-09-05 06:35:02 +00:00
|
|
|
wrongPubKey := secondPrivKey.PublicKey()
|
2019-08-27 13:29:42 +00:00
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
actual := wrongPubKey.Verify(signedData, hashedData.BytesBE())
|
2019-08-27 13:29:42 +00:00
|
|
|
expcted := false
|
|
|
|
assert.Equal(t, expcted, actual)
|
|
|
|
}
|