refactor: code optimization

This commit is contained in:
meilin.huang
2025-04-23 20:36:32 +08:00
parent 798ab7d18b
commit 2170509d92
33 changed files with 445 additions and 380 deletions

View File

@@ -12,10 +12,6 @@ import (
"encoding/hex"
"encoding/pem"
"errors"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/model"
"os"
"golang.org/x/crypto/bcrypt"
)
@@ -106,119 +102,6 @@ func RsaDecrypt(privateKeyStr string, data []byte) ([]byte, error) {
return rsa.DecryptPKCS1v15(rand.Reader, priv, data)
}
// 使用系统默认的私钥解密
// @param base64 字符串是否使用base64编码
func DefaultRsaDecrypt(data string, useBase64 bool) (string, error) {
// 空字符串不解密
if data == "" {
return "", nil
}
if useBase64 {
if decodeBase64, err := base64.StdEncoding.DecodeString(data); err != nil {
return "", err
} else {
data = string(decodeBase64)
}
}
priKey, err := GetRsaPrivateKey()
if err != nil {
return "", err
}
val, err := RsaDecrypt(priKey, []byte(data))
if err != nil {
return "", err
}
return string(val), nil
}
const (
// 公钥文件路径
publicKeyFile = "./mayfly_rsa.pub"
// 私钥文件路径
privateKeyFile = "./mayfly_rsa"
publicKeyK = "mayfly:public-key"
privateKeyK = "mayfly:private-key"
)
// 获取系统的RSA公钥
func GetRsaPublicKey() (string, error) {
if cache.UseRedisCache() {
publicKey := cache.GetStr(publicKeyK)
if publicKey != "" {
return publicKey, nil
}
} else {
content, err := os.ReadFile(publicKeyFile)
if err != nil {
publicKey := cache.GetStr(publicKeyK)
if publicKey != "" {
return publicKey, nil
}
} else {
return string(content), nil
}
}
_, pubKey, err := GenerateAndSaveRSAKey()
return pubKey, err
}
// 获取系统私钥
func GetRsaPrivateKey() (string, error) {
if cache.UseRedisCache() {
priKey := cache.GetStr(privateKeyK)
if priKey != "" {
return priKey, nil
}
} else {
content, err := os.ReadFile(privateKeyFile)
if err != nil {
priKey := cache.GetStr(privateKeyK)
if priKey != "" {
return priKey, nil
}
} else {
return string(content), nil
}
}
priKey, _, err := GenerateAndSaveRSAKey()
return priKey, err
}
// 生成并保存rsa key优先保存于磁盘若磁盘保存失败则保存至缓存
//
// 依次返回 privateKey, publicKey, error
func GenerateAndSaveRSAKey() (string, string, error) {
privateKey, publicKey, err := GenerateRSAKey(1024)
if err != nil {
return "", "", err
}
// 如果使用了redis缓存则优先存入redis
if cache.UseRedisCache() {
logx.Debug("系统配置了redis, rsa存入redis")
cache.SetStr(privateKeyK, privateKey, -1)
cache.SetStr(publicKeyK, publicKey, -1)
return privateKey, publicKey, nil
}
err = os.WriteFile(privateKeyFile, []byte(privateKey), 0644)
if err != nil {
logx.ErrorTrace("RSA私钥写入磁盘文件失败, 使用缓存存储该私钥", err)
cache.SetStr(privateKeyK, privateKey, -1)
}
err = os.WriteFile(publicKeyFile, []byte(publicKey), 0644)
if err != nil {
logx.ErrorTrace("RSA公钥写入磁盘文件失败, 使用缓存存储该公钥", err)
cache.SetStr(publicKeyK, publicKey, -1)
}
return privateKey, publicKey, nil
}
// AesEncrypt 加密
func AesEncrypt(data []byte, key []byte) ([]byte, error) {
//创建加密实例
@@ -281,12 +164,6 @@ func AesDecryptBase64(data string, key []byte) ([]byte, error) {
return AesDecrypt(dataByte, key)
}
func AesDecryptByLa(data string, la *model.LoginAccount) (string, error) {
key := []byte(la.GetAesKey())
res, err := AesDecryptBase64(data, key)
return string(res), err
}
// pkcs7Padding 填充
func pkcs7Padding(data []byte, blockSize int) []byte {
//判断缺少几位长度。最少1最多 blockSize