-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrsa.go
86 lines (77 loc) · 2.26 KB
/
rsa.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
package wxworkfinancesdk
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
)
// RSAEncrypt RSA加密
func RSAEncrypt(publicKey []byte, origData []byte) ([]byte, error) {
pubInterface, err := x509.ParsePKIXPublicKey(publicKey)
if err != nil {
return nil, err
}
pub := pubInterface.(*rsa.PublicKey)
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
// RSAEncryptBase64 RSA加密后base64encode
func RSAEncryptBase64(publicKey []byte, origData []byte) (string, error) {
encryptedData, err := RSAEncrypt(publicKey, origData)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(encryptedData), nil
}
// RSADecrypt RSA解密
func RSADecrypt(privateKey []byte, ciphertext []byte) ([]byte, error) {
priv, err := x509.ParsePKCS1PrivateKey(privateKey)
if err != nil {
return nil, err
}
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}
// RSADecryptBase64 base64decode后RSA解密
func RSADecryptBase64(privateKey []byte, cryptoText string) ([]byte, error) {
encryptedData, _ := base64.StdEncoding.DecodeString(cryptoText)
return RSADecrypt(privateKey, encryptedData)
}
// RSASignWithSHA1 RSA-SHA1签名
func RSASignWithSHA1(privateKey []byte, data []byte) ([]byte, error) {
priv, err := x509.ParsePKCS1PrivateKey(privateKey)
if err != nil {
return nil, err
}
//priv := privInterface.(*rsa.PrivateKey)
hash := Sha1Sum(data)
return rsa.SignPKCS1v15(rand.Reader, priv, crypto.SHA1, hash)
}
// RSASignWithSHA1Base64 RSA-SHA1签名后做base64encode
func RSASignWithSHA1Base64(privateKey []byte, data []byte) (string, error) {
sign, err := RSASignWithSHA1(privateKey, data)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(sign), nil
}
// RSAVerifySignWithSha1 RSA-SHA1签名验证
func RSAVerifySignWithSha1(publicKey []byte, origData []byte, signData string) error {
sign, err := base64.StdEncoding.DecodeString(signData)
if err != nil {
return err
}
pubInterface, err := x509.ParsePKIXPublicKey(publicKey)
if err != nil {
return err
}
pub := pubInterface.(*rsa.PublicKey)
hash := Sha1Sum(origData)
return rsa.VerifyPKCS1v15(pub, crypto.SHA1, hash, sign)
}
// Sha1Sum SHA1
func Sha1Sum(data []byte) []byte {
h := sha1.New()
h.Write(data)
return h.Sum(nil)
}