@@ -5,13 +5,47 @@ import (
55 "crypto"
66 "crypto/rsa"
77 "crypto/x509"
8- "encoding/pem"
98 "encoding/base64"
9+ "encoding/pem"
1010 "testing"
1111
1212 "github.com/stretchr/testify/assert"
1313)
1414
15+ func TestRSAGenerateKey (t * testing.T ) {
16+ priBuf := bytes .NewBuffer (nil )
17+ err := RSAGenerateKey (2048 , priBuf )
18+ assert .NoError (t , err )
19+ t .Logf ("private key: %s\n " , priBuf .Bytes ())
20+
21+ block , _ := pem .Decode (priBuf .Bytes ())
22+ assert .NotNil (t , block , "Failed to decode private key" )
23+ assert .Equal (t , "RSA PRIVATE KEY" , block .Type , "Invalid key type" )
24+
25+ _ , err = x509 .ParsePKCS1PrivateKey (block .Bytes )
26+ assert .NoError (t , err , "Failed to parse private key" )
27+ }
28+
29+ func TestRSAGeneratePublicKey (t * testing.T ) {
30+ priBuf := bytes .NewBuffer (nil )
31+ err := RSAGenerateKey (2048 , priBuf )
32+ assert .NoError (t , err )
33+
34+ pubBuf := bytes .NewBuffer (nil )
35+ err = RSAGeneratePublicKey (priBuf .Bytes (), pubBuf )
36+ assert .NoError (t , err )
37+ t .Logf ("public key: %s\n " , pubBuf .Bytes ())
38+
39+ block , _ := pem .Decode (pubBuf .Bytes ())
40+ assert .NotNil (t , block , "Failed to decode public key" )
41+ assert .Equal (t , "RSA PUBLIC KEY" , block .Type , "Invalid key type" )
42+
43+ pubKey , err := x509 .ParsePKIXPublicKey (block .Bytes )
44+ assert .NoError (t , err , "Failed to parse public key" )
45+ _ , ok := pubKey .(* rsa.PublicKey )
46+ assert .True (t , ok , "Key is not an RSA public key" )
47+ }
48+
1549func TestRSAEncrypt (t * testing.T ) {
1650 priBuf := bytes .NewBuffer (nil )
1751 err := RSAGenerateKey (2048 , priBuf )
@@ -55,35 +89,3 @@ func TestRSASign(t *testing.T) {
5589 err = RSAVerify (src , sign , pubBuf .Bytes (), crypto .SHA256 )
5690 assert .NoError (t , err )
5791}
58-
59- func TestRSAGenerateKey (t * testing.T ) {
60- priBuf := bytes .NewBuffer (nil )
61- err := RSAGenerateKey (2048 , priBuf )
62- assert .NoError (t , err )
63-
64- block , _ := pem .Decode (priBuf .Bytes ())
65- assert .NotNil (t , block , "Failed to decode private key" )
66- assert .Equal (t , "RSA PRIVATE KEY" , block .Type , "Invalid key type" )
67-
68- _ , err = x509 .ParsePKCS1PrivateKey (block .Bytes )
69- assert .NoError (t , err , "Failed to parse private key" )
70- }
71-
72- func TestRSAGeneratePublicKey (t * testing.T ) {
73- priBuf := bytes .NewBuffer (nil )
74- err := RSAGenerateKey (2048 , priBuf )
75- assert .NoError (t , err )
76-
77- pubBuf := bytes .NewBuffer (nil )
78- err = RSAGeneratePublicKey (priBuf .Bytes (), pubBuf )
79- assert .NoError (t , err )
80-
81- block , _ := pem .Decode (pubBuf .Bytes ())
82- assert .NotNil (t , block , "Failed to decode public key" )
83- assert .Equal (t , "RSA PUBLIC KEY" , block .Type , "Invalid key type" )
84-
85- pubKey , err := x509 .ParsePKIXPublicKey (block .Bytes )
86- assert .NoError (t , err , "Failed to parse public key" )
87- _ , ok := pubKey .(* rsa.PublicKey )
88- assert .True (t , ok , "Key is not an RSA public key" )
89- }
0 commit comments