Commit 3e58950d authored by shajiaiming's avatar shajiaiming

aes encrypt

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