修改对应app的参数结构

This commit is contained in:
474420502 2024-04-09 13:38:29 +08:00
parent 8b7b138ca2
commit 578d7ecdf9
16 changed files with 605 additions and 78 deletions

View File

@ -1,8 +1,37 @@
package account
import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
)
type RegisterWithEmailParam struct {
// randstr: 521546
// sign: 14AE4C5F153568828EBDFFF5C953D67F
// action: account/registerEmailCode
// app_market: 1
// email: 474420502@qq.com
// timestamp: 1712570272
// token: 3cf6b06db8491f4cc52fde5891165e8a
RandStr string `json:"randstr" form:"randstr"`
Sign string `json:"sign" form:"sign"`
Action string `json:"action" form:"action"`
AppMarket int `json:"app_market" form:"app_market"`
Email string `json:"email" form:"email"`
Timestamp int64 `json:"timestamp" form:"timestamp"`
Token string `json:"token" form:"token"`
}
// type RegisterWithEmailData struct {
// IsLogin bool `json:"is_login"`
// LangData struct {
// LanguageCode string `json:"language_code"`
// LanguageID string `json:"language_id"`
// } `json:"lang_data"`
// Token string `json:"token"`
// }
// 注册使用email
func RegisterWithEmail(ctx *gin.Context) {
func RegisterWithEmailCode(ctx *gin.Context) {
}

View File

@ -1,18 +0,0 @@
package handlers
import (
"github.com/gin-gonic/gin"
"github.com/iapologizewhenimwrong/Vestmore_GO/server/app/internal/handlers/actions"
)
var HandlersFuncRoutes map[string]gin.HandlerFunc = make(map[string]gin.HandlerFunc)
func init() {
HandlersFuncRoutes["account/forgetSmsCode"] = actions.AccountForgetSmsCode
HandlersFuncRoutes["account/loginWithEmailPassword"] = actions.AccountLoginWithEmailPassword
HandlersFuncRoutes["account/loginWithTelephonePassword"] = actions.AccountLoginWithTelephonePassword
HandlersFuncRoutes["account/registerEmailCode"] = actions.AccountRegisterEmailCode
HandlersFuncRoutes["account/registerSmsCode"] = actions.AccountRegisterSmsCode
HandlersFuncRoutes["base/getToken"] = actions.BaseGetToken
HandlersFuncRoutes["member/alterPassword"] = actions.MemberAlterPassword
}

View File

@ -4,6 +4,7 @@ import (
"log"
"github.com/gin-gonic/gin"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
)
// @Action base/getToken
@ -12,8 +13,7 @@ import (
// app_market: string;
// lang: string;
// token: string;
func BaseGetToken(ctx *gin.Context) {
param := &BaseGetTokenParam{}
func BaseGetToken(ctx *gin.Context, param *BaseGetTokenParam, resp *basic.Response) {
ctx.ShouldBind(param)
// model.Models.KillaraCustomerModel.Find()
log.Println()
@ -29,7 +29,7 @@ func BaseGetToken(ctx *gin.Context) {
// telephone: string;
// token?: string;
// version?: string;
func AccountLoginWithTelephonePassword(ctx *gin.Context) {
func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWithTelephonePasswordParam, resp *basic.Response) {
// ctx.ShouldBind()
// model.Models.KillaraCustomerModel.Find()
@ -42,7 +42,7 @@ func AccountLoginWithTelephonePassword(ctx *gin.Context) {
// country_code?: string;
// telephone?: string;
// token: string;
func AccountRegisterSmsCode(ctx *gin.Context) {
func AccountRegisterSmsCode(ctx *gin.Context, param *AccountRegisterSmsCodeParam, resp *basic.Response) {
// ctx.ShouldBind()
log.Println()
}
@ -53,20 +53,22 @@ func AccountRegisterSmsCode(ctx *gin.Context) {
// country_code?: string;
// telephone?: string;
// token: string;
func AccountForgetSmsCode(ctx *gin.Context) {
func AccountForgetSmsCode(ctx *gin.Context, param *AccountForgetSmsCodeParam, resp *basic.Response) {
// ctx.ShouldBind()
log.Println()
}
// @Action account/registerEmailCode
// AccountRegisterEmailCode
// action: string;
// email?: string;
// token: string;
func AccountRegisterEmailCode(ctx *gin.Context) {
// ctx.ShouldBind()
// randstr: string;
// sign: string;
// action: string;
// app_market: int;
// email: string;
// timestamp: int64;
// token: string;
func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeParam, resp *basic.Response) {
log.Println()
}
// @Action member/alterPassword
@ -76,21 +78,24 @@ func AccountRegisterEmailCode(ctx *gin.Context) {
// new_password: string;
// old_password: string;
// token?: string;
func MemberAlterPassword(ctx *gin.Context) {
func MemberAlterPassword(ctx *gin.Context, param *MemberAlterPasswordParam, resp *basic.Response) {
// ctx.ShouldBind()
log.Println()
}
// @Action account/loginWithEmailPassword
// AccountLoginWithEmailPassword
// action: string;
// device?: string;
// email: string;
// lang: string;
// password: string;
// token?: string;
// version?: string;
func AccountLoginWithEmailPassword(ctx *gin.Context) {
// ctx.ShouldBind()
log.Println()
// password: string;
// randstr: string;
// sign: string;
// action: string;
// device: string;
// version: string;
// app_market: int;
// email: string;
// timestamp: int64;
// token: string;
func AccountLoginWithEmailPassword(ctx *gin.Context, param *AccountLoginWithEmailPasswordParam, resp *basic.Response) {
log.Println(param)
}

View File

@ -1,5 +1,31 @@
package actions
import (
"log"
"github.com/gin-gonic/gin"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
)
var HandlersFuncRoutes map[string]gin.HandlerFunc = make(map[string]gin.HandlerFunc)
func init() {
// func AccountForgetSmsCode(ctx gin.Context, param AccountForgetSmsCodeParam, resp *basic.Response)
HandlersFuncRoutes["account/forgetSmsCode"] = AccountForgetSmsCodeHandler
// func AccountLoginWithEmailPassword(ctx gin.Context, param AccountLoginWithEmailPasswordParam, resp *basic.Response)
HandlersFuncRoutes["account/loginWithEmailPassword"] = AccountLoginWithEmailPasswordHandler
// func AccountLoginWithTelephonePassword(ctx gin.Context, param AccountLoginWithTelephonePasswordParam, resp *basic.Response)
HandlersFuncRoutes["account/loginWithTelephonePassword"] = AccountLoginWithTelephonePasswordHandler
// func AccountRegisterEmailCode(ctx gin.Context, param AccountRegisterEmailCodeParam, resp *basic.Response)
HandlersFuncRoutes["account/registerEmailCode"] = AccountRegisterEmailCodeHandler
// func AccountRegisterSmsCode(ctx gin.Context, param AccountRegisterSmsCodeParam, resp *basic.Response)
HandlersFuncRoutes["account/registerSmsCode"] = AccountRegisterSmsCodeHandler
// func BaseGetToken(ctx gin.Context, param BaseGetTokenParam, resp *basic.Response)
HandlersFuncRoutes["base/getToken"] = BaseGetTokenHandler
// func MemberAlterPassword(ctx gin.Context, param MemberAlterPasswordParam, resp *basic.Response)
HandlersFuncRoutes["member/alterPassword"] = MemberAlterPasswordHandler
}
type AccountForgetSmsCodeParam struct {
Action string `json:"action" form:"action" binding:"-"`
CountryCode string `json:"country_code" form:"country_code" binding:"required"`
@ -7,14 +33,47 @@ type AccountForgetSmsCodeParam struct {
Token string `json:"token" form:"token" binding:"-"`
}
func AccountForgetSmsCodeHandler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &AccountForgetSmsCodeParam{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
AccountForgetSmsCode(ctx, param, resp)
}
type AccountLoginWithEmailPasswordParam struct {
Action string `json:"action" form:"action" binding:"-"`
Device string `json:"device" form:"device" binding:"required"`
Email string `json:"email" form:"email" binding:"-"`
Lang string `json:"lang" form:"lang" binding:"-"`
Password string `json:"password" form:"password" binding:"-"`
Token string `json:"token" form:"token" binding:"required"`
Version string `json:"version" form:"version" binding:"required"`
Password string `json:"password" form:"password" binding:"-"`
Randstr string `json:"randstr" form:"randstr" binding:"-"`
Sign string `json:"sign" form:"sign" binding:"-"`
Action string `json:"action" form:"action" binding:"-"`
Device string `json:"device" form:"device" binding:"-"`
Version string `json:"version" form:"version" binding:"-"`
AppMarket int `json:"app_market" form:"app_market" binding:"-"`
Email string `json:"email" form:"email" binding:"-"`
Timestamp int64 `json:"timestamp" form:"timestamp" binding:"-"`
Token string `json:"token" form:"token" binding:"-"`
}
func AccountLoginWithEmailPasswordHandler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &AccountLoginWithEmailPasswordParam{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
AccountLoginWithEmailPassword(ctx, param, resp)
}
type AccountLoginWithTelephonePasswordParam struct {
@ -28,10 +87,44 @@ type AccountLoginWithTelephonePasswordParam struct {
Version string `json:"version" form:"version" binding:"required"`
}
func AccountLoginWithTelephonePasswordHandler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &AccountLoginWithTelephonePasswordParam{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
AccountLoginWithTelephonePassword(ctx, param, resp)
}
type AccountRegisterEmailCodeParam struct {
Action string `json:"action" form:"action" binding:"-"`
Email string `json:"email" form:"email" binding:"required"`
Token string `json:"token" form:"token" binding:"-"`
Randstr string `json:"randstr" form:"randstr" binding:"-"`
Sign string `json:"sign" form:"sign" binding:"-"`
Action string `json:"action" form:"action" binding:"-"`
AppMarket int `json:"app_market" form:"app_market" binding:"-"`
Email string `json:"email" form:"email" binding:"-"`
Timestamp int64 `json:"timestamp" form:"timestamp" binding:"-"`
Token string `json:"token" form:"token" binding:"-"`
}
func AccountRegisterEmailCodeHandler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &AccountRegisterEmailCodeParam{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
AccountRegisterEmailCode(ctx, param, resp)
}
type AccountRegisterSmsCodeParam struct {
@ -41,6 +134,21 @@ type AccountRegisterSmsCodeParam struct {
Token string `json:"token" form:"token" binding:"-"`
}
func AccountRegisterSmsCodeHandler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &AccountRegisterSmsCodeParam{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
AccountRegisterSmsCode(ctx, param, resp)
}
type BaseGetTokenParam struct {
Action string `json:"action" form:"action" binding:"-"`
AppMarket string `json:"app_market" form:"app_market" binding:"-"`
@ -48,6 +156,21 @@ type BaseGetTokenParam struct {
Token string `json:"token" form:"token" binding:"-"`
}
func BaseGetTokenHandler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &BaseGetTokenParam{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
BaseGetToken(ctx, param, resp)
}
type MemberAlterPasswordParam struct {
Action string `json:"action" form:"action" binding:"-"`
ConfirmPassword string `json:"confirm_password" form:"confirm_password" binding:"-"`
@ -55,3 +178,18 @@ type MemberAlterPasswordParam struct {
OldPassword string `json:"old_password" form:"old_password" binding:"-"`
Token string `json:"token" form:"token" binding:"required"`
}
func MemberAlterPasswordHandler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &MemberAlterPasswordParam{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
MemberAlterPassword(ctx, param, resp)
}

View File

@ -1,14 +1,2 @@
package handlers
import (
"github.com/gin-gonic/gin"
"github.com/iapologizewhenimwrong/Vestmore_GO/server/app/internal/handlers/actions"
)
var HandlersFuncRoutes map[string]gin.HandlerFunc = make(map[string]gin.HandlerFunc)
func init() {
{{- range .}}
HandlersFuncRoutes["{{.ActionName}}"] = actions.{{.FuncName}}
{{- end}}
}

View File

@ -37,7 +37,7 @@ func createActionRoutesGen() {
panic(err)
}
genFile(tpl, "gen_action_routes.tpl", "./action_routes_gen.go", af)
// genFile(tpl, "gen_action_routes.tpl", "./action_routes_gen.go", af)
genFile(tpl, "types_gen.tpl", "./actions/types_gen.go", af)
}

View File

@ -1,9 +1,38 @@
package actions
import (
"github.com/gin-gonic/gin"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
)
var HandlersFuncRoutes map[string]gin.HandlerFunc = make(map[string]gin.HandlerFunc)
func init() {
{{- range .}}
// func {{.FuncName}}(ctx *gin.Context, param *{{.ParamStruct.ParamStructName}}, resp *basic.Response)
HandlersFuncRoutes["{{.ActionName}}"] = {{.FuncName}}Handler
{{- end}}
}
{{- range .}}
type {{.ParamStruct.ParamStructName}} struct {
{{- range .ParamStruct.ParamFields}}
{{.ParamNameCamel}} {{.ParamType}} `json:"{{.ParamName}}" form:"{{.ParamName}}" binding:"{{.ParamBinding}}"`
{{- end}}
}
func {{.FuncName}}Handler(ctx *gin.Context) {
resp := &basic.Response{}
defer ctx.JSON(200, resp)
param := &{{.ParamStruct.ParamStructName}}{}
err := ctx.ShouldBind(param)
if err != nil {
log.Println(err)
resp.Error(basic.ErrParamParse)
return
}
{{.FuncName}}(ctx, param, resp)
}
{{end}}

View File

@ -5,39 +5,41 @@ import (
"github.com/gin-gonic/gin"
"github.com/iapologizewhenimwrong/Vestmore_GO/model"
"github.com/iapologizewhenimwrong/Vestmore_GO/server/app/internal/handlers"
"github.com/iapologizewhenimwrong/Vestmore_GO/server/app/internal/handlers/account"
"github.com/iapologizewhenimwrong/Vestmore_GO/server/app/internal/handlers/actions"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/cors"
_ "github.com/go-sql-driver/mysql"
)
func AppV1_0(ctx *gin.Context) {
if actionKey, ok := ctx.GetPostForm("action"); ok {
// if token, ok := ctx.GetPostForm("token"); ok {
log.Println(actionKey)
// }
handlers.HandlersFuncRoutes[actionKey](ctx)
actions.HandlersFuncRoutes[actionKey](ctx)
return
}
}
func main() {
log.SetFlags(log.Llongfile)
model.Models.SetSqlxDriver("mysql", "php:aFk3i4Dj#76!4sd@tcp(47.243.100.6:3306)/zunxinfinance?charset=utf8mb4&timeout=10s")
r := gin.Default()
cors.SetCors(r)
app_1_0 := r.Group("app")
account_1_0 := app_1_0.Group("account")
account_1_0.POST("/register_with_email", account.RegisterWithEmail)
account_1_0.POST("/register_with_email", account.RegisterWithEmailCode)
app_1_0.POST("/1_0", AppV1_0)
for k := range handlers.HandlersFuncRoutes {
for k := range actions.HandlersFuncRoutes {
log.Printf("api action %s", k)
}
r.Run(":8080")
r.Run(":9999")
}

View File

@ -1,16 +1,13 @@
package main
import (
"context"
"testing"
"github.com/iapologizewhenimwrong/Vestmore_GO/model"
_ "github.com/go-sql-driver/mysql"
)
func TestMain(t *testing.T) {
model.Models.SetSqlxDriver("mysql", "php:aFk3i4Dj#76!4sd@tcp(47.243.100.6:3306)/zunxinfinance?parseTime=true&charset=utf8mb4&timeout=10s")
model.Models.KillaraCustomerModel.Find(context.TODO())
// model.Models.SetSqlxDriver("mysql", "php:aFk3i4Dj#76!4sd@tcp(47.243.100.6:3306)/zunxinfinance?parseTime=true&charset=utf8mb4&timeout=10s")
// model.Models.KillaraCustomerModel.Find(context.TODO())
main()
}

10
utils/basic/error_code.go Normal file
View File

@ -0,0 +1,10 @@
package basic
type ErrorCode struct {
Code int `json:"code"`
Message string `json:"message"`
}
var (
ErrParamParse = &ErrorCode{Code: 10100, Message: "参数解析错误"}
)

41
utils/basic/types.go Normal file
View File

@ -0,0 +1,41 @@
package basic
// 全局返回的结构体
type Response struct {
Data interface{} `json:"data"`
ErrorCode int `json:"error_code"`
ErrorText string `json:"error_text"`
IsSuccess bool `json:"success"`
}
func (resp *Response) Error(errcode *ErrorCode, Data ...interface{}) {
resp.ErrorCode = errcode.Code
resp.ErrorText = errcode.Message
resp.IsSuccess = false
resp.setData(Data)
}
func (resp *Response) ErrorEx(Code int, Message string, Data ...interface{}) {
resp.ErrorCode = Code
resp.ErrorText = Message
resp.IsSuccess = false
resp.setData(Data)
}
func (resp *Response) Success(Data ...interface{}) {
resp.IsSuccess = true
resp.setData(Data)
}
func (resp *Response) setData(Data []interface{}) {
if len(Data) == 0 {
resp.Data = []interface{}{}
return
}
if len(Data) == 1 {
resp.Data = Data
} else {
resp.Data = Data
}
}

27
utils/cors/cors.go Normal file
View File

@ -0,0 +1,27 @@
package cors
import (
"net/http"
"github.com/gin-gonic/gin"
)
func SetCors(router *gin.Engine) {
// 处理前端发送的options请求
router.OPTIONS("/*path", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*") // 可以根据实际情况改为特定域名
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
c.Header("Access-Control-Allow-Credentials", "true")
c.Status(http.StatusOK)
})
// 处理所有的路由请求
router.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*") // 允许所有域名跨域访问,也可以指定特定的域名
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
c.Header("Access-Control-Allow-Credentials", "true")
c.Next()
})
}

View File

@ -0,0 +1,4 @@
package encryption_decryption
// 必须16字节
var cbckey = "bjwl20240409"

View File

@ -0,0 +1,106 @@
package encryption_decryption
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/gob"
"sync"
)
type ISecretEncDec interface {
EncodeToString([]byte) string
DecodeString(string) ([]byte, error)
}
type SecretCRT[T any] struct {
srcKey string
secretKey []byte
iv []byte
derivationKey func(keysting string) []byte
mu sync.Mutex
EncDec ISecretEncDec
}
func NewSecretCRT[T any](key string, iv string) *SecretCRT[T] {
if key == "" {
panic("NewSecretCRT key must not null")
}
s := &SecretCRT[T]{
derivationKey: DerivationKeyV1,
iv: []byte(iv),
EncDec: base64.RawURLEncoding,
}
s.secretKey = s.derivationKey(key)
return s
}
func (s *SecretCRT[T]) UpdateDerivationKeyFunc(kfunc func(keysting string) []byte) {
s.mu.Lock()
defer s.mu.Unlock()
s.derivationKey = kfunc
s.secretKey = s.derivationKey(s.srcKey)
}
func (s *SecretCRT[T]) Encrypt(gobj *T) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
var buf = bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(gobj)
if err != nil {
return "", err
}
// 使用AES加密,返回一个Block接口
block, err := aes.NewCipher(s.secretKey)
if err != nil {
panic(err)
}
// 使用CTR模式
stream := cipher.NewCTR(block, s.iv)
// 加密明文
plaintext := buf.Bytes()
ciphertext := make([]byte, len(plaintext))
stream.XORKeyStream(ciphertext, plaintext)
// 转为hex编码打印出来
return s.EncDec.EncodeToString(ciphertext), nil
}
func (s *SecretCRT[T]) Decrypt(ciphertext string) (*T, error) {
// 将hex解码成[]byte
ciphertextbytes, err := s.EncDec.DecodeString(ciphertext)
if err != nil {
return nil, err
}
// 生成Block接口
block, err := aes.NewCipher(s.secretKey)
if err != nil {
panic(err)
}
// 生成CTR模式
stream := cipher.NewCTR(block, s.iv)
// 解密密文
plaintext := make([]byte, len(ciphertextbytes))
stream.XORKeyStream(plaintext, ciphertextbytes)
// 解出golang的结构体
var protected T
var buf = bytes.NewBuffer(plaintext)
err = gob.NewDecoder(buf).Decode(&protected)
if err != nil {
return nil, err
}
return &protected, nil
}

View File

@ -0,0 +1,133 @@
package encryption_decryption
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/gob"
"fmt"
"io"
"sync"
)
func DerivationKeyV1(keysting string) []byte {
key := []byte(keysting)
var result [16]byte
// If key length is more than 32, truncate it
if len(key) > 16 {
key = key[:16]
}
// If key length is less than 32, replicate it until it reaches 32
for len(key) < 16 {
key = append(key, key...)
}
// Only take the first 32 bytes
key = key[:16]
// Swap the first 16 bytes with the last 16 bytes
copy(result[:], key[8:])
copy(result[8:], key[:8])
return result[:]
}
type SecretGCM[T any] struct {
srcKey string
secretKey []byte
derivationKey func(keysting string) []byte
mu sync.Mutex
EncDec ISecretEncDec
}
func NewSecretGCM[T any](key string) *SecretGCM[T] {
if key == "" {
panic("NewSecretGCM key must not null")
}
s := &SecretGCM[T]{
srcKey: key,
derivationKey: DerivationKeyV1,
EncDec: base64.RawURLEncoding,
}
s.secretKey = s.derivationKey(s.srcKey)
return s
}
func (s *SecretGCM[T]) UpdateDerivationKeyFunc(kfunc func(keysting string) []byte) {
s.mu.Lock()
defer s.mu.Unlock()
s.derivationKey = kfunc
s.secretKey = s.derivationKey(s.srcKey)
}
func (s *SecretGCM[T]) Encrypt(gobj *T) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
var buf = bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(gobj)
if err != nil {
return "", err
}
block, err := aes.NewCipher(s.secretKey)
if err != nil {
return "", err
}
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
ciphertext := aesgcm.Seal(nonce, nonce, buf.Bytes(), nil)
return s.EncDec.EncodeToString(ciphertext), nil
}
func (s *SecretGCM[T]) Decrypt(ciphertext string) (*T, error) {
block, err := aes.NewCipher(s.secretKey)
if err != nil {
return nil, err
}
ct, err := s.EncDec.DecodeString(ciphertext)
if err != nil {
return nil, err
}
if len(ct) < 12 {
return nil, fmt.Errorf("ciphertext too short")
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
plaintext, err := aesgcm.Open(nil, ct[:12], ct[12:], nil)
if err != nil {
return nil, err
}
// 解出golang的结构体
var protected T
var buf = bytes.NewBuffer(plaintext)
err = gob.NewDecoder(buf).Decode(&protected)
if err != nil {
return nil, err
}
return &protected, nil
}

View File

@ -0,0 +1,36 @@
package encryption_decryption
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"sort"
"strings"
)
func MakeSign(params map[string]interface{}) string {
// 排序
keys := make([]string, len(params))
i := 0
for k, _ := range params {
keys[i] = k
i++
}
sort.Strings(keys)
byteBuf := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(byteBuf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(params)
if err != nil {
panic(err)
}
data := byteBuf.String()
h := md5.New()
h.Write([]byte(strings.TrimRight(data, "\n")))
return hex.EncodeToString(h.Sum(nil))
}