11package utils
22
33import (
4- "bytes"
5- "crypto"
6- "crypto/rand"
74 "crypto/rsa"
85 "crypto/x509"
6+ "encoding/base64"
97 "encoding/pem"
108 "errors"
119 "strings"
@@ -20,10 +18,6 @@ const (
2018
2119 kPKCS8Prefix = "-----BEGIN PRIVATE KEY-----"
2220 KPKCS8Suffix = "-----END PRIVATE KEY-----"
23-
24- kPublicKeyType = "PUBLIC KEY"
25- kPrivateKeyType = "PRIVATE KEY"
26- kRSAPrivateKeyType = "RSA PRIVATE KEY"
2721)
2822
2923var (
3226)
3327
3428func FormatPublicKey (raw string ) []byte {
29+ newRaw , err := base64 .StdEncoding .DecodeString (raw )
30+ if err == nil {
31+ raw = string (newRaw )
32+ }
3533 return formatKey (raw , kPublicKeyPrefix , kPublicKeySuffix , 64 )
3634}
3735
@@ -75,209 +73,24 @@ func ParsePKCS8PrivateKey(data []byte) (key *rsa.PrivateKey, err error) {
7573 }
7674
7775 key , ok := rawKey .(* rsa.PrivateKey )
78- if ok == false {
76+ if ! ok {
7977 return nil , ErrPrivateKeyFailedToLoad
8078 }
8179
8280 return key , err
8381}
8482
85- func ParsePublicKey (data []byte ) (key * rsa. PublicKey , err error ) {
83+ func ParsePublicKey (data []byte ) (key any , err error ) {
8684 var block * pem.Block
8785 block , _ = pem .Decode (data )
8886 if block == nil {
8987 return nil , ErrPublicKeyFailedToLoad
9088 }
9189
92- var pubInterface interface {}
93- pubInterface , err = x509 .ParsePKIXPublicKey (block .Bytes )
90+ key , err = x509 .ParsePKIXPublicKey (block .Bytes )
9491 if err != nil {
9592 return nil , err
9693 }
97- key , ok := pubInterface .(* rsa.PublicKey )
98- if ! ok {
99- return nil , ErrPublicKeyFailedToLoad
100- }
10194
10295 return key , err
10396}
104-
105- func packageData (data []byte , packageSize int ) (r [][]byte ) {
106- src := make ([]byte , len (data ))
107- copy (src , data )
108-
109- r = make ([][]byte , 0 )
110- if len (src ) <= packageSize {
111- return append (r , src )
112- }
113- for len (src ) > 0 {
114- p := src [:packageSize ]
115- r = append (r , p )
116- src = src [packageSize :]
117- if len (src ) <= packageSize {
118- r = append (r , src )
119- break
120- }
121- }
122- return r
123- }
124-
125- // RSAEncrypt 使用公钥 key 对数据 data 进行 RSA 加密
126- func RSAEncrypt (plaintext , key []byte ) ([]byte , error ) {
127- pubKey , err := ParsePublicKey (key )
128- if err != nil {
129- return nil , err
130- }
131-
132- return RSAEncryptWithKey (plaintext , pubKey )
133- }
134-
135- // RSAEncryptWithKey 使用公钥 key 对数据 data 进行 RSA 加密
136- func RSAEncryptWithKey (plaintext []byte , key * rsa.PublicKey ) ([]byte , error ) {
137- pData := packageData (plaintext , key .N .BitLen ()/ 8 - 11 )
138- ciphertext := make ([]byte , 0 , 0 )
139-
140- for _ , d := range pData {
141- c , e := rsa .EncryptPKCS1v15 (rand .Reader , key , d )
142- if e != nil {
143- return nil , e
144- }
145- ciphertext = append (ciphertext , c ... )
146- }
147-
148- return ciphertext , nil
149- }
150-
151- // RSADecryptWithPKCS1 使用私钥 key 对数据 data 进行 RSA 解密,key 的格式为 pkcs1
152- func RSADecryptWithPKCS1 (ciphertext , key []byte ) ([]byte , error ) {
153- priKey , err := ParsePKCS1PrivateKey (key )
154- if err != nil {
155- return nil , err
156- }
157-
158- return RSADecryptWithKey (ciphertext , priKey )
159- }
160-
161- // RSADecryptWithPKCS8 使用私钥 key 对数据 data 进行 RSA 解密,key 的格式为 pkcs8
162- func RSADecryptWithPKCS8 (ciphertext , key []byte ) ([]byte , error ) {
163- priKey , err := ParsePKCS8PrivateKey (key )
164- if err != nil {
165- return nil , err
166- }
167-
168- return RSADecryptWithKey (ciphertext , priKey )
169- }
170-
171- // RSADecryptWithKey 使用私钥 key 对数据 data 进行 RSA 解密
172- func RSADecryptWithKey (ciphertext []byte , key * rsa.PrivateKey ) ([]byte , error ) {
173- pData := packageData (ciphertext , key .PublicKey .N .BitLen ()/ 8 )
174- plaintext := make ([]byte , 0 , 0 )
175-
176- for _ , d := range pData {
177- p , e := rsa .DecryptPKCS1v15 (rand .Reader , key , d )
178- if e != nil {
179- return nil , e
180- }
181- plaintext = append (plaintext , p ... )
182- }
183- return plaintext , nil
184- }
185-
186- func RSASignWithPKCS1 (plaintext , key []byte , hash crypto.Hash ) ([]byte , error ) {
187- priKey , err := ParsePKCS1PrivateKey (key )
188- if err != nil {
189- return nil , err
190- }
191- return RSASignWithKey (plaintext , priKey , hash )
192- }
193-
194- func RSASignWithPKCS8 (plaintext , key []byte , hash crypto.Hash ) ([]byte , error ) {
195- priKey , err := ParsePKCS8PrivateKey (key )
196- if err != nil {
197- return nil , err
198- }
199- return RSASignWithKey (plaintext , priKey , hash )
200- }
201-
202- func RSASignWithKey (plaintext []byte , key * rsa.PrivateKey , hash crypto.Hash ) ([]byte , error ) {
203- h := hash .New ()
204- h .Write (plaintext )
205- hashed := h .Sum (nil )
206- return rsa .SignPKCS1v15 (rand .Reader , key , hash , hashed )
207- }
208-
209- func RSAVerify (ciphertext , sign , key []byte , hash crypto.Hash ) error {
210- pubKey , err := ParsePublicKey (key )
211- if err != nil {
212- return err
213- }
214- return RSAVerifyWithKey (ciphertext , sign , pubKey , hash )
215- }
216-
217- func RSAVerifyWithKey (ciphertext , sign []byte , key * rsa.PublicKey , hash crypto.Hash ) error {
218- h := hash .New ()
219- h .Write (ciphertext )
220- hashed := h .Sum (nil )
221- return rsa .VerifyPKCS1v15 (key , hash , hashed , sign )
222- }
223-
224- func getPublicKeyBytes (publicKey * rsa.PublicKey ) ([]byte , error ) {
225- pubDer , err := x509 .MarshalPKIXPublicKey (publicKey )
226- if err != nil {
227- return nil , err
228- }
229-
230- pubBlock := & pem.Block {Type : kPublicKeyType , Bytes : pubDer }
231-
232- var pubBuf bytes.Buffer
233- if err = pem .Encode (& pubBuf , pubBlock ); err != nil {
234- return nil , err
235- }
236- return pubBuf .Bytes (), nil
237- }
238-
239- func GenRSAKeyWithPKCS1 (bits int ) (privateKey , publicKey []byte , err error ) {
240- priKey , err := rsa .GenerateKey (rand .Reader , bits )
241- if err != nil {
242- return nil , nil , err
243- }
244- priDer := x509 .MarshalPKCS1PrivateKey (priKey )
245- priBlock := & pem.Block {Type : kRSAPrivateKeyType , Bytes : priDer }
246-
247- var priBuf bytes.Buffer
248- if err = pem .Encode (& priBuf , priBlock ); err != nil {
249- return nil , nil , err
250- }
251-
252- publicKey , err = getPublicKeyBytes (& priKey .PublicKey )
253- if err != nil {
254- return nil , nil , err
255- }
256- privateKey = priBuf .Bytes ()
257- return privateKey , publicKey , err
258- }
259-
260- func GenRSAKeyWithPKCS8 (bits int ) (privateKey , publicKey []byte , err error ) {
261- priKey , err := rsa .GenerateKey (rand .Reader , bits )
262- if err != nil {
263- return nil , nil , err
264- }
265- priDer , err := x509 .MarshalPKCS8PrivateKey (priKey )
266- if err != nil {
267- return nil , nil , err
268- }
269- priBlock := & pem.Block {Type : kPrivateKeyType , Bytes : priDer }
270-
271- var priBuf bytes.Buffer
272- if err = pem .Encode (& priBuf , priBlock ); err != nil {
273- return nil , nil , err
274- }
275-
276- publicKey , err = getPublicKeyBytes (& priKey .PublicKey )
277- if err != nil {
278- return nil , nil , err
279- }
280- privateKey = priBuf .Bytes ()
281-
282- return privateKey , publicKey , err
283- }
0 commit comments