-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrsa_test.go
87 lines (67 loc) · 2.42 KB
/
rsa_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
package crypto_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/republicprotocol/crypto-go"
)
var _ = Describe("Rsa keys", func() {
Context("when generating", func() {
It("should be able to generate a random RsaPrivateKey without returning an error", func() {
_, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
})
It("should equal itself", func() {
key, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
Expect(key.Equal(&key)).Should(BeTrue())
})
It("should not equal another randomly generated RsaPrivateKey", func() {
key1, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
key2, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
Expect(key1.Equal(&key2)).Should(BeFalse())
})
})
Context("when encrypting and decrypting", func() {
It("should be able to encrypt a plain text message", func() {
key, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
plainText := []byte("REN")
_, err = key.PublicKey().Encrypt(plainText)
Expect(err).ShouldNot(HaveOccurred())
})
It("should be able to decrypt an encrypted cipher text", func() {
key, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
plainText := []byte("REN")
cipherText, err := key.PublicKey().Encrypt(plainText)
Expect(err).ShouldNot(HaveOccurred())
plainTextDecrypted, err := key.Decrypt(cipherText)
Expect(err).ShouldNot(HaveOccurred())
Expect(plainText).Should(Equal(plainTextDecrypted))
})
})
Context("when marshaling and unmarshaling", func() {
It("should be able to marshal and unmarshal as JSON", func() {
key, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
data, err := key.MarshalJSON()
Expect(err).ShouldNot(HaveOccurred())
keyDecoded := RsaPrivateKey{}
err = keyDecoded.UnmarshalJSON(data)
Expect(err).ShouldNot(HaveOccurred())
Expect(key.Equal(&keyDecoded)).Should(BeTrue())
})
It("should be able to marshal and unmarshal public keys as bytes", func() {
key, err := RandomRsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
data, err := key.PublicKey().Bytes()
Expect(err).ShouldNot(HaveOccurred())
publicKey, err := NewRsaPublicKeyFromBytes(data)
Expect(err).ShouldNot(HaveOccurred())
Expect(key.N).Should(Equal(publicKey.N))
Expect(key.E).Should(Equal(publicKey.E))
})
})
})