1 Star 0 Fork 0

xlizy/common-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sign.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
xlizy 提交于 2024-04-09 16:16 . init
package crypto
import (
"fmt"
"gitee.com/xlizy/common-go/base/config"
"gitee.com/xlizy/common-go/base/const"
"gitee.com/xlizy/common-go/utils/json"
"sort"
"time"
)
func GenSign(obj any, app string) string {
m := make(map[string]any)
keys := make([]string, 0)
jsonStr := json.ToJsonStr(obj)
json.ToObj(jsonStr, &m)
for k, _ := range m {
if k != "sign" {
keys = append(keys, k)
}
}
sort.Strings(keys)
o := ""
for _, k := range keys {
o += k
o += fmt.Sprintf("%v", m[k])
}
if config.AppSign.Sign[app] == "" {
o += "W4v0s1yoyvpYADUHeNpgbrTpu6a2cgEC"
} else {
o += config.AppSign.Sign[app]
}
return Md5(o)
}
func CheckSign(obj any, app string) bool {
m := make(map[string]any)
keys := make([]string, 0)
jsonStr := json.ToJsonStr(obj)
json.ToObj(jsonStr, &m)
for k, _ := range m {
if k != "sign" {
keys = append(keys, k)
}
}
sort.Strings(keys)
o := ""
for _, k := range keys {
o += k
o += fmt.Sprintf("%v", m[k])
}
if app == "" {
o += "W4v0s1yoyvpYADUHeNpgbrTpu6a2cgEC"
} else {
o += config.AppSign.Sign[app]
}
sign := Md5(o)
if m["timestamp"] != "" {
t, e := time.Parse(constant.DataFormat, fmt.Sprintf("%v", m["timestamp"]))
if e != nil {
return false
}
if time.Now().After(t.Add(5 * time.Minute)) {
return false
}
}
if m["sign"] == sign {
return true
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xlizy/common-go.git
git@gitee.com:xlizy/common-go.git
xlizy
common-go
common-go
v0.1.9

搜索帮助

0d507c66 1850385 C8b1a773 1850385