-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsam_test.go
159 lines (147 loc) · 4.32 KB
/
rsam_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package rsam_test
import (
"bytes"
"crypto/sha256"
"fmt"
"testing"
"github.com/gossl/rsam"
)
func TestPublicKeySignature(t *testing.T) {
priv, pub, err := rsam.GeneratePairKeys(1024)
if err != nil {
t.Error(err)
}
signature, err := rsam.SignWithPublicKey([]byte("test"), pub)
if err != nil {
t.Error(err)
}
err = rsam.VerifyWithPrivateKey([]byte("test"), signature, priv)
if err != nil {
t.Error(err)
}
}
func TestPublicKeySignatureInvalid(t *testing.T) {
priv, pub, err := rsam.GeneratePairKeys(1024)
if err != nil {
t.Error(err)
}
signature, err := rsam.SignWithPublicKey([]byte("test"), pub)
if err != nil {
t.Error(err)
}
err = rsam.VerifyWithPrivateKey([]byte("test2"), signature, priv)
if err == nil {
t.Error(err)
}
}
func TestPrivateKeySignature(t *testing.T) {
priv, pub, err := rsam.GeneratePairKeys(1024)
if err != nil {
t.Error()
}
signature, err := rsam.SignWithPrivateKey([]byte("test"), priv)
if err != nil {
t.Error()
}
err = rsam.VerifyWithPublicKey([]byte("test"), signature, pub)
if err != nil {
t.Error()
}
}
func TestPrivateKeySignatureInvalid(t *testing.T) {
priv, pub, err := rsam.GeneratePairKeys(1024)
if err != nil {
t.Error()
}
signature, err := rsam.SignWithPrivateKey([]byte("test"), priv)
if err != nil {
t.Error()
}
err = rsam.VerifyWithPublicKey([]byte("te2st"), signature, pub)
if err == nil {
t.Error()
}
}
func TestGeneratePairKeys(t *testing.T) {
priv, pub, err := rsam.GeneratePairKeys(1024)
if err != nil {
t.Error(err)
}
if priv == nil || pub == nil {
t.Error("keys are nil")
}
}
func TestPublicKeyEncryption(t *testing.T) {
priv, pub, _ := rsam.GeneratePairKeys(2048)
msg := []byte("hello world")
encrypted, err := rsam.EncryptWithPublicKey(msg, pub, sha256.New())
if err != nil {
t.Error(err)
}
decrypted, err := rsam.DecryptWithPrivateKey(encrypted, priv, sha256.New())
if err != nil {
t.Error(err)
}
if !bytes.Equal(msg, decrypted) {
t.Error("decrypted message not equal to original message")
}
}
func TestPublicKeyEncryptionLongMessage(t *testing.T) {
priv, pub, _ := rsam.GeneratePairKeys(2048)
rep := 1000
msg := []byte("hello world")
for i := 0; i < rep; i++ {
msg = append(msg, []byte("hello world")...)
}
encrypted, err := rsam.EncryptWithPublicKey(msg, pub, sha256.New())
if err != nil {
t.Error(err)
}
decrypted, err := rsam.DecryptWithPrivateKey(encrypted, priv, sha256.New())
if err != nil {
t.Error(err)
}
if !bytes.Equal(msg, decrypted) {
t.Error("decrypted message not equal to original message")
}
}
func TestPrivateKeyEncryption(t *testing.T) {
priv, pub, _ := rsam.GeneratePairKeys(2048)
// priv, _ := rsam.BytesToPrivateKey([]byte(staticPrivateKey))
// pub, _ := rsam.BytesToPublicKey([]byte(staticPublicKey))
fmt.Println(priv.Size(), pub.Size())
msg := []byte("hello world 22")
encrypted, err := rsam.EncryptWithPrivateKey(msg, priv, sha256.New())
if err != nil {
t.Error(err)
}
decrypted, err := rsam.DecryptWithPublicKey(encrypted, pub, sha256.New())
if err != nil {
t.Error(err)
}
// fmt.Println(string(decrypted))
if !bytes.Equal(msg, decrypted) {
t.Error("decrypted message not equal to original message")
}
}
var staticPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQD1/618vsRezcClpPzD4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