Commit 3e58950d authored by shajiaiming's avatar shajiaiming

aes encrypt

parent 43c1bc85
......@@ -5,8 +5,10 @@ import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/xinliangnote/go-util/aes"
"net/url"
"sort"
"strconv"
......@@ -49,7 +51,7 @@ func verifySign(c *gin.Context) (map[string]string, error) {
// 验证来源
value, ok := config.ApiAuthConfig[ak]
if ok {
AppSecret = value["md5"]
AppSecret = value["aes"]
} else {
return nil, errno.ErrAk
}
......@@ -57,9 +59,14 @@ func verifySign(c *gin.Context) (map[string]string, error) {
if debug == "1" {
currentUnix := util.GetCurrentUnix()
req.Set("ts", strconv.FormatInt(currentUnix, 10))
sn, err := createSign(req)
if err != nil {
return nil, errno.ErrSn
}
res := map[string]string{
"ts": strconv.FormatInt(currentUnix, 10),
"sn": createSign(req),
"sn": sn,
}
return res, nil
}
......@@ -73,17 +80,23 @@ func verifySign(c *gin.Context) (map[string]string, error) {
}
// 验证签名
if sn == "" || sn != createSign(req) {
if sn == "" {
return nil, errno.ErrSn
}
decryptStr, decryptErr := aes.Decrypt(sn, []byte(AppSecret), AppSecret)
if decryptErr != nil {
return nil, errors.New(decryptErr.Error())
}
if decryptStr != createEncryptStr(req) {
return nil, errno.ErrSn
}
return nil, nil
}
// 创建签名
func createSign(params url.Values) string {
// 自定义 MD5 组合
return util.EncodeMD5(AppSecret + createEncryptStr(params) + AppSecret)
func createSign(params url.Values) (string, error) {
return aes.Encrypt(createEncryptStr(params), []byte(AppSecret), AppSecret)
}
func createEncryptStr(params url.Values) string {
......
......@@ -28,6 +28,8 @@ func InitRouter() *gin.Engine {
//r.POST("/upload", api.UploadImage)
client := r.Group("/interface")
client.Use(aes.Aes())
client.GET("/news", app.GetNews)
client.GET("/one-news", app.GetOneNews)
client.GET("/articles", app.GetArticle)
......@@ -35,7 +37,7 @@ func InitRouter() *gin.Engine {
client.GET("/live-categories", app.GetLiveCategories)
client.GET("/transaction-fee", app.GetTransactionFee)
client.GET("/app/expired", app.AppExpired)
client.Use(aes.Aes())
client.POST("/user", app.AddUser)
api := r.Group("/api")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment