mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
添加企业微信会话存档SDK (#419)
* 添加企业微信会话存档SDK * 更新说明文档 * 更新包名为msgaudit并更新说明文档 * 迁移会话存档SDK到work目录下 * 移动RSA文件到util并添加动态库文件 * 整合企业微信和会话存档配置文件 * 修复golangcli-lint提示中的错误 * 对整个项目进行gofmt * 更新会话存档说明文档 * 会话存档消息获取是抛出error * 更新会话存档说明文档 Co-authored-by: Afeyer <afeyer@h5base.cn>
This commit is contained in:
43
util/rsa.go
Normal file
43
util/rsa.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// RSADecrypt 数据解密
|
||||
func RSADecrypt(privateKey string, ciphertext []byte) ([]byte, error) {
|
||||
block, _ := pem.Decode([]byte(privateKey))
|
||||
if block == nil {
|
||||
return nil, errors.New("PrivateKey format error")
|
||||
}
|
||||
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
oldErr := err
|
||||
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ParsePKCS1PrivateKey error: %s, ParsePKCS8PrivateKey error: %s", oldErr.Error(), err.Error())
|
||||
}
|
||||
switch t := key.(type) {
|
||||
case *rsa.PrivateKey:
|
||||
priv = key.(*rsa.PrivateKey)
|
||||
default:
|
||||
return nil, fmt.Errorf("ParsePKCS1PrivateKey error: %s, ParsePKCS8PrivateKey error: Not supported privatekey format, should be *rsa.PrivateKey, got %T", oldErr.Error(), t)
|
||||
}
|
||||
}
|
||||
return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
|
||||
}
|
||||
|
||||
// RSADecryptBase64 Base64解码后再次进行RSA解密
|
||||
func RSADecryptBase64(privateKey string, cryptoText string) ([]byte, error) {
|
||||
encryptedData, err := base64.StdEncoding.DecodeString(cryptoText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return RSADecrypt(privateKey, encryptedData)
|
||||
}
|
||||
Reference in New Issue
Block a user