-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrsa_test.go
96 lines (90 loc) · 2.82 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
88
89
90
91
92
93
94
95
96
package rencrypto_test
import (
"crypto/rand"
"crypto/rsa"
"reflect"
"testing/quick"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/renproject/rencrypto"
)
var _ = Describe("RSA", func() {
Context("when marshalling and unmarshalling the encrypter", func() {
It("should not change", func() {
Expect(quick.Check(func() bool {
key, err := rsa.GenerateKey(rand.Reader, 2048)
Expect(err).Should(BeNil())
encrypter, err := NewRSAEncrypter(key)
Expect(err).Should(BeNil())
data, err := encrypter.Marshal()
Expect(err).Should(BeNil())
encrypter2, err := NewRSAEncrypter(data)
Expect(err).Should(BeNil())
data2, err := encrypter2.Marshal()
Expect(err).Should(BeNil())
return reflect.DeepEqual(data, data2)
}, &quick.Config{
MaxCount: 8,
})).Should(BeNil())
})
It("should be able to encrypt a message", func() {
Expect(quick.Check(func() bool {
key, err := rsa.GenerateKey(rand.Reader, 2048)
Expect(err).Should(BeNil())
encrypter, err := NewRSAEncrypter(key)
Expect(err).Should(BeNil())
data, err := encrypter.Marshal()
Expect(err).Should(BeNil())
_, err = encrypter.Encrypt([]byte("Secret"))
Expect(err).Should(BeNil())
encrypter2, err := NewRSAEncrypter(data)
Expect(err).Should(BeNil())
_, err = encrypter2.Encrypt([]byte("Secret"))
Expect(err).Should(BeNil())
return true
}, &quick.Config{
MaxCount: 8,
})).Should(BeNil())
})
})
Context("when marshalling and unmarshalling the decrypter", func() {
It("should not change", func() {
Expect(quick.Check(func() bool {
key, err := rsa.GenerateKey(rand.Reader, 2048)
Expect(err).Should(BeNil())
decrypter, err := NewRSADecrypter(key)
Expect(err).Should(BeNil())
data, err := decrypter.Marshal()
Expect(err).Should(BeNil())
decrypter2, err := NewRSADecrypter(data)
Expect(err).Should(BeNil())
data2, err := decrypter2.Marshal()
Expect(err).Should(BeNil())
return reflect.DeepEqual(data, data2)
}, &quick.Config{
MaxCount: 8,
})).Should(BeNil())
})
It("should be able to decrypt a message", func() {
Expect(quick.Check(func() bool {
key, err := rsa.GenerateKey(rand.Reader, 2048)
Expect(err).Should(BeNil())
decrypter, err := NewRSADecrypter(key)
Expect(err).Should(BeNil())
cipherText, err := decrypter.Encrypter().Encrypt([]byte("Secret"))
Expect(err).Should(BeNil())
data, err := decrypter.Marshal()
Expect(err).Should(BeNil())
data1, err := decrypter.Decrypt(cipherText)
Expect(err).Should(BeNil())
decrypter2, err := NewRSADecrypter(data)
Expect(err).Should(BeNil())
data2, err := decrypter2.Decrypt(cipherText)
Expect(err).Should(BeNil())
return reflect.DeepEqual(data1, data2)
}, &quick.Config{
MaxCount: 8,
})).Should(BeNil())
})
})
})