-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkey_test.go
executable file
·141 lines (96 loc) · 3.35 KB
/
key_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package crypto_test
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/alinz/crypto.go"
)
func TestEncodeDecodeKey(t *testing.T) {
bannerName := "AWESOME"
public, private, err := crypto.NewKeyPair()
assert.NoError(t, err)
t.Run("should encode and decode public key in bytes", func(t *testing.T) {
encoded, err := public.Encode()
assert.NoError(t, err)
result := &crypto.PublicKey{}
err = result.Decode(encoded)
assert.NoError(t, err)
assert.Equal(t, public, result)
})
t.Run("should encode and decode public key in PEM format", func(t *testing.T) {
var buffer bytes.Buffer
var err error
err = crypto.EncodePEM(&buffer, public, bannerName, nil)
assert.NoError(t, err)
result := &crypto.PublicKey{}
err = crypto.DecodePEM(&buffer, result, bannerName, nil)
assert.NoError(t, err)
assert.EqualValues(t, public, result)
})
t.Run("should encode and decode private key in PEM format", func(t *testing.T) {
var buffer bytes.Buffer
err := crypto.EncodePEM(&buffer, private, bannerName, nil)
assert.NoError(t, err)
result := &crypto.PrivateKey{}
err = crypto.DecodePEM(&buffer, result, bannerName, nil)
assert.NoError(t, err)
assert.Equal(t, private, result)
})
t.Run("should encode and decode private key with passphrase in PEM format", func(t *testing.T) {
var buffer bytes.Buffer
key := make([]byte, 32)
err := crypto.EncodePEM(&buffer, private, bannerName, key)
assert.NoError(t, err)
result := &crypto.PrivateKey{}
err = crypto.DecodePEM(&buffer, result, bannerName, key)
assert.NoError(t, err)
assert.Equal(t, private, result)
})
t.Run("should encode and failed to decode because of wrong passphrase", func(t *testing.T) {
var buffer bytes.Buffer
key := make([]byte, 32)
err := crypto.EncodePEM(&buffer, private, bannerName, key)
assert.NoError(t, err)
result := &crypto.PrivateKey{}
key[0] = 1 // changed the key
err = crypto.DecodePEM(&buffer, result, bannerName, key)
assert.Error(t, err)
})
t.Run("should encode and decode private key in bytes", func(t *testing.T) {
encoded, err := private.Encode()
assert.NoError(t, err)
result := &crypto.PrivateKey{}
err = result.Decode(encoded)
assert.NoError(t, err)
assert.Equal(t, private, result)
})
t.Run("should sign and verify the content", func(t *testing.T) {
message1 := []byte("hello world")
message2 := []byte("byte world")
signature, err := private.Sign(message1)
assert.NoError(t, err)
assert.True(t, public.Verify(message1, signature))
assert.False(t, public.Verify(message2, signature))
})
t.Run("should generate the same shared key", func(t *testing.T) {
pub1, priv1, err := crypto.NewKeyPair()
assert.NoError(t, err)
pub2, priv2, err := crypto.NewKeyPair()
assert.NoError(t, err)
sharedKey1, err := priv1.SharedKey(pub2)
assert.NoError(t, err)
sharedKey2, err := priv2.SharedKey(pub1)
assert.NoError(t, err)
assert.Equal(t, sharedKey1, sharedKey2)
})
}
func TestIsKeysMatched(t *testing.T) {
public1, private1, err := crypto.NewKeyPair()
assert.NoError(t, err)
public2, private2, err := crypto.NewKeyPair()
assert.NoError(t, err)
assert.True(t, crypto.IsKeysMatched(public1, private1))
assert.False(t, crypto.IsKeysMatched(public1, private2))
assert.False(t, crypto.IsKeysMatched(public2, private1))
assert.True(t, crypto.IsKeysMatched(public2, private2))
}