|
3 | 3 | package utils |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "crypto/rand" |
| 7 | + "crypto/rsa" |
| 8 | + "crypto/sha1" |
| 9 | + "crypto/x509" |
| 10 | + "encoding/base64" |
| 11 | + "fmt" |
6 | 12 | "testing" |
7 | 13 |
|
8 | 14 | "github.com/stretchr/testify/assert" |
@@ -67,34 +73,73 @@ fHMq4tsbKO0dKAeydPM/nrUZBmaYQVKMVOORGLFjFKVO7JV6Kq/R86ouhjEPgJOe |
67 | 73 | 2xulNBUcjicqtZlBdEh/PWCYP2SpGVDclKm8jeo175T3EVAkdKzzmfpxtMmnMlmq |
68 | 74 | cTJOU9TxuGvNASMtjj7pYIerTx+xgZDXEVBWFW9PjJ0TV06tCRsgSHItgg== |
69 | 75 | -----END CERTIFICATE-----` |
| 76 | + testRSACryptoUtilPrivateKey *rsa.PrivateKey |
| 77 | + testRSACryptoUtilPublicKey *rsa.PublicKey |
| 78 | + testRSACryptoUtilCertificate *x509.Certificate |
70 | 79 | ) |
71 | 80 |
|
72 | | -func TestEncryptAndDecrypt(t *testing.T) { |
73 | | - privatKey, err := LoadPrivateKey(testingKey(testRSACryptoUtilPrivateKeyStr)) |
| 81 | +func init() { |
| 82 | + var err error |
| 83 | + testRSACryptoUtilPrivateKey, err = LoadPrivateKey(testingKey(testRSACryptoUtilPrivateKeyStr)) |
| 84 | + if err != nil { |
| 85 | + panic(fmt.Errorf("fail to load the private key:%s", err.Error())) |
| 86 | + } |
| 87 | + testRSACryptoUtilPublicKey, err = LoadPublicKey(testRSACryptoUtilPublicKeyStr) |
| 88 | + if err != nil { |
| 89 | + panic(fmt.Errorf("fail to load the public key:%s", err.Error())) |
| 90 | + } |
| 91 | + testRSACryptoUtilCertificate, err = LoadCertificate(testRSACryptoUtilMchCertificateStr) |
| 92 | + if err != nil { |
| 93 | + panic(fmt.Errorf("fail to load the certificate key:%s", err.Error())) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestOAEPCrypto(t *testing.T) { |
| 98 | + |
| 99 | + const message = "hello world" |
| 100 | + // 使用OAEP padding方式对证书加密 |
| 101 | + ciphertext, err := EncryptOAEPWithCertificate(message, testRSACryptoUtilCertificate) |
74 | 102 | require.NoError(t, err) |
75 | 103 |
|
76 | | - publicKey, err := LoadPublicKey(testRSACryptoUtilPublicKeyStr) |
| 104 | + // 使用OAEP padding方式用公有库直接进行私钥解密,以验证加密正确 |
| 105 | + decodedCiphertext, err := base64.StdEncoding.DecodeString(ciphertext) |
77 | 106 | require.NoError(t, err) |
| 107 | + decryptMessageBytes, err := rsa.DecryptOAEP( |
| 108 | + sha1.New(), rand.Reader, testRSACryptoUtilPrivateKey, decodedCiphertext, nil) |
| 109 | + require.NoError(t, err) |
| 110 | + assert.Equal(t, message, string(decryptMessageBytes)) |
78 | 111 |
|
79 | | - certificate, err := LoadCertificate(testRSACryptoUtilMchCertificateStr) |
| 112 | + // 使用OAEP padding方式直接公钥加密 |
| 113 | + ciphertext, err = EncryptOAEPWithPublicKey(message, testRSACryptoUtilPublicKey) |
80 | 114 | require.NoError(t, err) |
81 | 115 |
|
| 116 | + // 使用OAEP padding方式私钥解密 |
| 117 | + decryptMessage, err := DecryptOAEP(ciphertext, testRSACryptoUtilPrivateKey) |
| 118 | + require.NoError(t, err) |
| 119 | + assert.Equal(t, message, decryptMessage) |
| 120 | +} |
| 121 | + |
| 122 | +func TestPKCS1v15Crypto(t *testing.T) { |
| 123 | + |
82 | 124 | const message = "hello world" |
83 | | - // 使用证书加密 |
84 | | - cipertext, err := EncryptOAEPWithCertificate(message, certificate) |
| 125 | + |
| 126 | + // 使用PKCS1 padding对证书加密 |
| 127 | + ciphertext, err := EncryptPKCS1v15WithCertificate(message, testRSACryptoUtilCertificate) |
85 | 128 | require.NoError(t, err) |
86 | 129 |
|
87 | | - // 私钥解密 |
88 | | - decryptMessage, err := DecryptOAEP(cipertext, privatKey) |
| 130 | + // 使用PKCS1 padding对用公有库直接进行私钥解密,以验证加密正确 |
| 131 | + decodedCiphertext, err := base64.StdEncoding.DecodeString(ciphertext) |
89 | 132 | require.NoError(t, err) |
90 | | - assert.Equal(t, message, decryptMessage) |
| 133 | + decryptMessageBytes, err := rsa.DecryptPKCS1v15(rand.Reader, testRSACryptoUtilPrivateKey, decodedCiphertext) |
| 134 | + require.NoError(t, err) |
| 135 | + assert.Equal(t, message, string(decryptMessageBytes)) |
91 | 136 |
|
92 | | - // 直接公钥加密 |
93 | | - cipertext, err = EncryptOAEPWithPublicKey(message, publicKey) |
| 137 | + // 使用PKCS1 padding进行公钥加密 |
| 138 | + ciphertext, err = EncryptPKCS1v15WithPublicKey(message, testRSACryptoUtilPublicKey) |
94 | 139 | require.NoError(t, err) |
95 | 140 |
|
96 | | - // 私钥解密 |
97 | | - decryptMessage, err = DecryptOAEP(cipertext, privatKey) |
| 141 | + // 使用PKCS1 padding进行私钥解密 |
| 142 | + decryptMessage, err := DecryptPKCS1v15(ciphertext, testRSACryptoUtilPrivateKey) |
98 | 143 | require.NoError(t, err) |
99 | 144 | assert.Equal(t, message, decryptMessage) |
100 | 145 | } |
0 commit comments