1 Star 0 Fork 0

余济舟/util

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
secret.go 3.83 KB
一键复制 编辑 原始数据 按行查看 历史
package secret
import (
"crypto/md5"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"hash"
"gitee.com/jericho-yu/util/v2/zip"
"gitee.com/jericho-yu/util/v2/common"
"gitee.com/jericho-yu/util/v2/secret/symmetric"
"gitee.com/jericho-yu/util/v2/str"
)
type Secret struct{}
var SecretHelper Secret
func (Secret) New() *Secret { return &Secret{} }
func (Secret) EncryptAuthorization(key, secretKey string, iv []byte, randStr ...string) (string, string, error) {
var (
err error
uuid = ""
token []byte
)
if key == "" {
return "", "", err
}
// 生成随机串
if len(randStr) > 0 {
uuid = randStr[0]
} else {
uuid, err = SecretHelper.MustEncrypt(str.GetLetters(10))
if err != nil {
return "", "", err
}
}
token, err = symmetric.CbcHelper.New().Encrypt([]byte(key+uuid), []byte(secretKey), iv)
if err != nil {
return "", "", err
}
return base64.StdEncoding.EncodeToString(token), uuid, nil
}
func (Secret) DecryptAuthorization(token, secretKey string, iv []byte) (string, string, error) {
var (
err error
token64, decryptToken []byte
)
if token == "" {
return "", "", errors.New("token 不能为空")
}
token64, err = base64.StdEncoding.DecodeString(token)
if err != nil {
return "", "", fmt.Errorf("base64解码token失败:%s", err.Error())
}
decryptToken, err = symmetric.CbcHelper.New().Decrypt(token64, []byte(secretKey), iv)
if err != nil {
return "", "", fmt.Errorf("解密失败:%s", err.Error())
}
return string(decryptToken[:len(decryptToken)-32]), string(decryptToken[len(decryptToken)-32:]), nil
}
func (Secret) MustEncrypt(data any) (string, error) {
var (
err error
dataBytes []byte
h hash.Hash
)
dataBytes = common.ToBytes(data)
h = md5.New()
if _, err = h.Write(dataBytes); err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
func (Secret) Ecb16Encrypt(data any, needEncrypt, needZip bool, aes *symmetric.Aes) (string, error) {
var (
jsonByte, b []byte
jsonMarshalErr, zipErr, encryptErr error
)
// json序列化
jsonByte, jsonMarshalErr = json.Marshal(data)
if jsonMarshalErr != nil {
return "", jsonMarshalErr
}
// 压缩
if needZip {
b, zipErr = zip.ZlibHelper.New().Compress(jsonByte)
if zipErr != nil {
return "", zipErr
}
}
// 加密
if needEncrypt {
b, encryptErr = symmetric.EcbHelper.New().Encrypt(b, aes.Encrypt.GetAesKey())
if encryptErr != nil {
return "", encryptErr
}
}
if !needZip && !needEncrypt {
return string(b), nil
} else {
return base64.StdEncoding.EncodeToString(b), nil
}
}
func (Secret) Ecb16Decrypt(data string, needEncrypt, needZip bool, aes *symmetric.Aes) (any, error) {
var (
r any
cipherText, decryptedByte, decompressedByte []byte
base64DecodeErr, jsonUnmarshalErr, decryptErr, zipErr error
)
if needEncrypt {
// base64 解码
cipherText, base64DecodeErr = base64.StdEncoding.DecodeString(data)
if base64DecodeErr != nil {
return nil, base64DecodeErr
}
// aes解密:ecb
decryptedByte, decryptErr = symmetric.EcbHelper.New().Decrypt(cipherText, aes.Encrypt.GetAesKey())
if decryptErr != nil {
return nil, decryptErr
}
// 解压
if needZip {
decompressedByte, zipErr = zip.ZlibHelper.New().Decompress(decryptedByte)
if zipErr != nil {
return nil, zipErr
}
jsonUnmarshalErr = json.Unmarshal(decompressedByte, &r)
if jsonUnmarshalErr != nil {
return nil, jsonUnmarshalErr
}
return r, nil
} else {
// 将data反序列化
jsonUnmarshalErr = json.Unmarshal(decryptedByte, &r)
if jsonUnmarshalErr != nil {
return nil, jsonUnmarshalErr
}
return r, nil
}
} else {
jsonUnmarshalErr = json.Unmarshal([]byte(data), &r)
if jsonUnmarshalErr != nil {
return nil, jsonUnmarshalErr
}
}
return r, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jericho-yu/util.git
git@gitee.com:jericho-yu/util.git
jericho-yu
util
util
v2.18.2

搜索帮助