All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
feat(dict): 添加字典数据压缩加密功能 - 在字典数据接口中集成压缩和加密机制 - 实现Gzip压缩、XOR加密和Base64编码流程 - 新增CompressAndEncrypt和DecryptAndDecompress工具函数 - 在middleware中启用压缩中间件支持 ```
123 lines
2.8 KiB
Go
123 lines
2.8 KiB
Go
package admin
|
||
|
||
import (
|
||
"bytes"
|
||
"compress/gzip"
|
||
"context"
|
||
"encoding/base64"
|
||
"fmt"
|
||
|
||
"blazing/cool"
|
||
|
||
"blazing/modules/dict/service"
|
||
|
||
"github.com/gogf/gf/v2/encoding/gjson"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
)
|
||
|
||
type DictInfoController struct {
|
||
*cool.Controller
|
||
}
|
||
|
||
func init() {
|
||
var dict_info_controller = &DictInfoController{
|
||
&cool.Controller{
|
||
Prefix: "/admin/dict/info",
|
||
Api: []string{"Add", "Delete", "Update", "Info", "List", "Page"},
|
||
Service: service.NewDictInfoService(),
|
||
},
|
||
}
|
||
// 注册路由
|
||
cool.RegisterController(dict_info_controller)
|
||
}
|
||
|
||
// Data 方法请求
|
||
type DictInfoDataReq struct {
|
||
g.Meta `path:"/data" method:"POST"`
|
||
Types []string `json:"types"`
|
||
}
|
||
|
||
// Data 方法 获得字典数据
|
||
func (c *DictInfoController) Data(ctx context.Context, req *DictInfoDataReq) (res *cool.BaseRes, err error) {
|
||
service := service.NewDictInfoService()
|
||
data, _ := service.Data(ctx, req.Types)
|
||
r, _ := gjson.EncodeString(data)
|
||
encryptedStr, _ := CompressAndEncrypt(r)
|
||
res = cool.Ok(encryptedStr)
|
||
return
|
||
}
|
||
|
||
// 异或密钥(两端必须一致)
|
||
const xorKey = "simple_key_123"
|
||
|
||
// Xor 异或加解密(复用同一个函数)
|
||
func Xor(data []byte) []byte {
|
||
keyLen := len(xorKey)
|
||
result := make([]byte, len(data))
|
||
for i := 0; i < len(data); i++ {
|
||
result[i] = data[i] ^ xorKey[i%keyLen]
|
||
}
|
||
return result
|
||
}
|
||
|
||
// GzipCompress Gzip压缩
|
||
func GzipCompress(data []byte) ([]byte, error) {
|
||
var buf bytes.Buffer
|
||
gz := gzip.NewWriter(&buf)
|
||
_, err := gz.Write(data)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if err := gz.Close(); err != nil {
|
||
return nil, err
|
||
}
|
||
return buf.Bytes(), nil
|
||
}
|
||
|
||
// GzipDecompress Gzip解压
|
||
func GzipDecompress(data []byte) ([]byte, error) {
|
||
buf := bytes.NewBuffer(data)
|
||
gz, err := gzip.NewReader(buf)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer gz.Close()
|
||
|
||
var res bytes.Buffer
|
||
_, err = res.ReadFrom(gz)
|
||
return res.Bytes(), err
|
||
}
|
||
|
||
// CompressAndEncrypt 压缩+异或+Base64
|
||
func CompressAndEncrypt(plainText string) (string, error) {
|
||
// 1. 字符串转字节数组
|
||
rawData := []byte(plainText)
|
||
// 2. Gzip压缩
|
||
compressed, err := GzipCompress(rawData)
|
||
if err != nil {
|
||
return "", fmt.Errorf("压缩失败:%v", err)
|
||
}
|
||
// 3. 异或加密
|
||
encrypted := Xor(compressed)
|
||
// 4. Base64编码
|
||
return base64.StdEncoding.EncodeToString(encrypted), nil
|
||
}
|
||
|
||
// DecryptAndDecompress Base64+异或+解压
|
||
func DecryptAndDecompress(encryptedStr string) (string, error) {
|
||
// 1. Base64解码
|
||
encryptedData, err := base64.StdEncoding.DecodeString(encryptedStr)
|
||
if err != nil {
|
||
return "", fmt.Errorf("Base64解码失败:%v", err)
|
||
}
|
||
// 2. 异或解密
|
||
decrypted := Xor(encryptedData)
|
||
// 3. Gzip解压
|
||
decompressed, err := GzipDecompress(decrypted)
|
||
if err != nil {
|
||
return "", fmt.Errorf("解压失败:%v", err)
|
||
}
|
||
// 4. 字节数组转字符串
|
||
return string(decompressed), nil
|
||
}
|