@@ -96,6 +96,64 @@ func TestDecryptOAEP(t *testing.T) {
96
96
}
97
97
}
98
98
99
+ func TestSignPKCS1v15 (t * testing.T ) {
100
+ privateKey , publicKey , err := GenRSAKey (RsaKeyBits2048 )
101
+ assert .Nil (t , err )
102
+ tests := []struct {
103
+ name string
104
+ message []byte
105
+ privKey []byte
106
+ wantErr bool
107
+ }{
108
+ {"should pass with correct private key" , []byte ("hello, world" ), privateKey , false },
109
+ {"should return err with incorrect private key" , []byte ("hello, world" ), []byte ("error_privateKey" ), true },
110
+ {"should pass with empty message" , []byte ("hello, world" ), privateKey , false },
111
+ }
112
+ for _ , tt := range tests {
113
+ t .Run (tt .name , func (t * testing.T ) {
114
+ sig , err := SignPKCS1v15 (tt .message , tt .privKey )
115
+ if tt .wantErr {
116
+ assert .NotNil (t , err )
117
+ return
118
+ } else {
119
+ assert .Nil (t , err )
120
+ }
121
+ err = VerifyPKCS1v15 (sig , tt .message , publicKey )
122
+ assert .Nil (t , err )
123
+ })
124
+ }
125
+ }
126
+
127
+ func TestVerifyPKCS1v15 (t * testing.T ) {
128
+ privateKey , publicKey , err := GenRSAKey (RsaKeyBits2048 )
129
+ assert .Nil (t , err )
130
+ message := []byte ("hello, world" )
131
+ sig , err := SignPKCS1v15 (message , privateKey )
132
+ assert .Nil (t , err )
133
+ tests := []struct {
134
+ name string
135
+ signature []byte
136
+ message []byte
137
+ pubKey []byte
138
+ wantErr bool
139
+ }{
140
+ {"should pass with correct message and signature" , sig , message , publicKey , false },
141
+ {"should return error with incorrect signature" , []byte ("error_signature" ), message , publicKey , true },
142
+ {"should return error with incorrect message" , sig , []byte ("error_message" ), publicKey , true },
143
+ {"should return error with incorrect public key" , sig , message , []byte ("error_public_key" ), true },
144
+ }
145
+ for _ , tt := range tests {
146
+ t .Run (tt .name , func (t * testing.T ) {
147
+ err = VerifyPKCS1v15 (tt .signature , tt .message , tt .pubKey )
148
+ if tt .wantErr {
149
+ assert .NotNil (t , err )
150
+ return
151
+ }
152
+ assert .Nil (t , err )
153
+ })
154
+ }
155
+ }
156
+
99
157
func TestEncryptPKCS1v15 (t * testing.T ) {
100
158
privKey , pubKey , err := GenRSAKey (1024 )
101
159
assert .Nil (t , err )
0 commit comments