Some checks failed
Build and Deploy (service.xpcool.com) / build-and-deploy (push) Failing after 5m7s
- 新增 9 张房屋相关数据表(社区/楼宇/房源/价格快照/交易/设施/社区设施/学区/偏好) - 添加菜单权限种子数据并绑定超级管理员角色 - 生成 DAO 层代码和实体对象 - 实现房屋模块 API 接口(社区/房源/看板)和控制器服务层 - 支持多平台软关联匹配、笋盘标记和低可信度标记功能 - 更新超级管理员账号为 xxcool/xxCool@2026 - 调整 RBAC 菜单结构,移除管理员管理功能,新增日志管理菜单 - 修复 RBAC 安全漏洞,确保禁用角色权限失效 - 重构认证模块,将登录相关接口迁移到统一包结构下 - 移除废弃的管理模块和工具类接口定义 - 为通用工具包添加中文注释和文档说明
81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
// Package cryptox 提供 AES/DES 加解密工具(base64 输出),
|
||
// 基于 gaes 与 gdes 构建。
|
||
//
|
||
// 密钥:接受任意长度密钥,内部会归一化为精确密钥尺寸,
|
||
// 调用方无需关心密钥长度。
|
||
package cryptox
|
||
|
||
import (
|
||
"crypto/md5"
|
||
"encoding/base64"
|
||
|
||
"github.com/gogf/gf/v2/crypto/gaes"
|
||
"github.com/gogf/gf/v2/crypto/gdes"
|
||
"github.com/gogf/gf/v2/errors/gerror"
|
||
)
|
||
|
||
const (
|
||
aesKeySize = 16 // AES-128 key size in bytes
|
||
desKeySize = 8 // DES key size in bytes
|
||
)
|
||
|
||
// normalizeKey 从任意长度密钥派生出固定尺寸密钥。
|
||
func normalizeKey(secret string, size int) []byte {
|
||
key := []byte(secret)
|
||
if len(key) == size {
|
||
return key
|
||
}
|
||
sum := md5.Sum([]byte(secret))
|
||
out := make([]byte, size)
|
||
for i := 0; i < size; i++ {
|
||
out[i] = sum[i%len(sum)]
|
||
}
|
||
return out
|
||
}
|
||
|
||
// AesEncrypt 使用由 secret 派生的密钥对 plainText 做 AES-128-CBC 加密,
|
||
// 返回 base64 编码的密文。
|
||
func AesEncrypt(plainText, secret string) (string, error) {
|
||
out, err := gaes.Encrypt([]byte(plainText), normalizeKey(secret, aesKeySize))
|
||
if err != nil {
|
||
return "", gerror.Wrap(err, `AesEncrypt failed`)
|
||
}
|
||
return base64.StdEncoding.EncodeToString(out), nil
|
||
}
|
||
|
||
// AesDecrypt 解密 AesEncrypt 产生的 base64 密文。
|
||
func AesDecrypt(cipherText, secret string) (string, error) {
|
||
data, err := base64.StdEncoding.DecodeString(cipherText)
|
||
if err != nil {
|
||
return "", gerror.Wrap(err, `base64 decode failed`)
|
||
}
|
||
out, err := gaes.Decrypt(data, normalizeKey(secret, aesKeySize))
|
||
if err != nil {
|
||
return "", gerror.Wrap(err, `AesDecrypt failed`)
|
||
}
|
||
return string(out), nil
|
||
}
|
||
|
||
// DesEncrypt 使用由 secret 派生的密钥对 plainText 做 DES-ECB(PKCS5 填充)加密,
|
||
// 返回 base64 编码的密文。
|
||
func DesEncrypt(plainText, secret string) (string, error) {
|
||
out, err := gdes.EncryptECB([]byte(plainText), normalizeKey(secret, desKeySize), gdes.PKCS5PADDING)
|
||
if err != nil {
|
||
return "", gerror.Wrap(err, `DesEncrypt failed`)
|
||
}
|
||
return base64.StdEncoding.EncodeToString(out), nil
|
||
}
|
||
|
||
// DesDecrypt 解密 DesEncrypt 产生的 base64 密文。
|
||
func DesDecrypt(cipherText, secret string) (string, error) {
|
||
data, err := base64.StdEncoding.DecodeString(cipherText)
|
||
if err != nil {
|
||
return "", gerror.Wrap(err, `base64 decode failed`)
|
||
}
|
||
out, err := gdes.DecryptECB(data, normalizeKey(secret, desKeySize), gdes.PKCS5PADDING)
|
||
if err != nil {
|
||
return "", gerror.Wrap(err, `DesDecrypt failed`)
|
||
}
|
||
return string(out), nil
|
||
}
|