-
Notifications
You must be signed in to change notification settings - Fork 8
/
key_pair_test.go
63 lines (58 loc) · 1.7 KB
/
key_pair_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package ecies
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestKeyPair(t *testing.T) {
}
func TestGenerateKey(t *testing.T) {
{
privateKey, err := GenerateKey()
assert.Nil(t, err)
assert.NotNil(t, privateKey)
assert.NotNil(t, privateKey.PublicKey)
assert.NotNil(t, privateKey.D)
assert.NotNil(t, privateKey.PublicKey.Curve)
assert.NotNil(t, privateKey.PublicKey.X)
assert.NotNil(t, privateKey.PublicKey.Y)
assert.Equal(t, curve, privateKey.Curve)
}
{
k, _ := GenerateKey()
privateKeyString := HexEncode(SerializePrivateKey(k))
publicKeyString := HexEncode(SerializePublicKey(k.PublicKey))
assert.NotNil(t, publicKeyString)
assert.NotNil(t, privateKeyString)
}
}
func TestKeySerialization(t *testing.T) {
k, err := GenerateKey()
assert.Nil(t, err)
{
publicKeyBytes := SerializePublicKey(k.PublicKey)
assert.NotNil(t, publicKeyBytes)
publicKey, err := DeserializePublicKey(publicKeyBytes)
assert.Nil(t, err)
assert.NotNil(t, publicKey)
assert.True(t, bytesEquals(k.X.Bytes(), publicKey.X.Bytes()))
assert.True(t, bytesEquals(k.Y.Bytes(), publicKey.Y.Bytes()))
}
{
x, y := SerializePublicKeyToCoordinate(k.PublicKey)
assert.NotNil(t, x)
assert.NotNil(t, y)
publicKey, err := DeserializePublicKeyFromCoordinate(x, y)
assert.Nil(t, err)
assert.NotNil(t, publicKey)
assert.True(t, bytesEquals(k.X.Bytes(), publicKey.X.Bytes()))
assert.True(t, bytesEquals(k.Y.Bytes(), publicKey.Y.Bytes()))
}
{
privateKeyBytes := SerializePrivateKey(k)
assert.NotNil(t, privateKeyBytes)
privateKey := DeserializePrivateKey(privateKeyBytes)
assert.Nil(t, err)
assert.NotNil(t, privateKey)
assert.True(t, bytesEquals(k.D.Bytes(), privateKey.D.Bytes()))
}
}