-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathecdsa_test.go
101 lines (94 loc) · 2.88 KB
/
ecdsa_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
package rencrypto_test
import (
"crypto/rand"
"reflect"
"testing/quick"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/renproject/rencrypto"
"github.com/ethereum/go-ethereum/crypto"
)
var _ = Describe("ECDSA", func() {
Context("when marshalling and unmarshalling the encryptor", func() {
It("should not change", func() {
Expect(quick.Check(func() bool {
key, err := crypto.GenerateKey()
Expect(err).Should(BeNil())
verifier, err := NewECDSAVerifier(key)
Expect(err).Should(BeNil())
data, err := verifier.Marshal()
Expect(err).Should(BeNil())
verifier2, err := NewECDSAVerifier(data)
Expect(err).Should(BeNil())
data2, err := verifier2.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 := crypto.GenerateKey()
Expect(err).Should(BeNil())
hash := [32]byte{}
rand.Read(hash[:])
sig, err := crypto.Sign(hash[:], key)
Expect(err).Should(BeNil())
verifier, err := NewECDSAVerifier(key)
Expect(err).Should(BeNil())
data, err := verifier.Marshal()
Expect(err).Should(BeNil())
Expect(verifier.Verify(sig, hash[:])).Should(BeNil())
verifier2, err := NewECDSAVerifier(data)
Expect(err).Should(BeNil())
Expect(verifier2.Verify(sig, hash[:])).Should(BeNil())
return true
}, &quick.Config{
MaxCount: 8,
})).Should(BeNil())
})
})
Context("when marshalling and unmarshalling the decryptor", func() {
It("should not change", func() {
Expect(quick.Check(func() bool {
key, err := crypto.GenerateKey()
Expect(err).Should(BeNil())
decryptor, err := NewECDSASigner(key)
Expect(err).Should(BeNil())
data, err := decryptor.Marshal()
Expect(err).Should(BeNil())
decryptor2, err := NewECDSASigner(data)
Expect(err).Should(BeNil())
data2, err := decryptor2.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 := crypto.GenerateKey()
Expect(err).Should(BeNil())
signer, err := NewECDSASigner(key)
Expect(err).Should(BeNil())
hash := [32]byte{}
rand.Read(hash[:])
data, err := signer.Marshal()
Expect(err).Should(BeNil())
sig, err := signer.Sign(hash[:])
Expect(err).Should(BeNil())
signer2, err := NewECDSASigner(data)
Expect(err).Should(BeNil())
sig1, err := signer2.Sign(hash[:])
Expect(err).Should(BeNil())
Expect(signer.Verifier().Verify(sig, hash[:])).Should(BeNil())
Expect(signer.Verifier().Verify(sig1, hash[:])).Should(BeNil())
return true
}, &quick.Config{
MaxCount: 8,
})).Should(BeNil())
})
})
})