代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。