feat: 支持redis缓存权限等信息

This commit is contained in:
meilin.huang
2022-12-26 20:53:07 +08:00
parent 4fec38724d
commit 4a26bb3ba5
11 changed files with 200 additions and 86 deletions

View File

@@ -12,6 +12,7 @@ import (
"encoding/hex"
"encoding/pem"
"errors"
"mayfly-go/pkg/cache"
"golang.org/x/crypto/bcrypt"
)
@@ -34,9 +35,6 @@ func CheckPwdHash(password, hash string) bool {
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
}
// 系统统一RSA秘钥对
var RsaPair []string
// 生成RSA私钥和公钥字符串
// bits 证书大小
// @return privateKeyStr publicKeyStr error
@@ -130,33 +128,36 @@ func DefaultRsaDecrypt(data string, useBase64 bool) (string, error) {
return string(val), nil
}
const publicKeyK = "mayfly:public-key"
const privateKeyK = "mayfly:private-key"
// 获取系统的RSA公钥
func GetRsaPublicKey() (string, error) {
if len(RsaPair) == 2 {
return RsaPair[1], nil
publicKey := cache.GetStr(publicKeyK)
if publicKey != "" {
return publicKey, nil
}
privateKey, publicKey, err := GenerateRSAKey(1024)
if err != nil {
return "", err
}
RsaPair = append(RsaPair, privateKey)
RsaPair = append(RsaPair, publicKey)
cache.SetStr(publicKeyK, publicKey)
cache.SetStr(privateKeyK, privateKey)
return publicKey, nil
}
// 获取系统私钥
func GetRsaPrivateKey() (string, error) {
if len(RsaPair) == 2 {
return RsaPair[0], nil
privateKey := cache.GetStr(privateKeyK)
if privateKey != "" {
return privateKey, nil
}
privateKey, publicKey, err := GenerateRSAKey(1024)
if err != nil {
return "", err
}
RsaPair = append(RsaPair, privateKey)
RsaPair = append(RsaPair, publicKey)
cache.SetStr(publicKeyK, publicKey)
cache.SetStr(privateKeyK, privateKey)
return privateKey, nil
}