66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package store
|
|
|
|
import (
|
|
"91porn-server/app/appg"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
func GetStoreLink(u *UserData) string {
|
|
if u == nil {
|
|
return ""
|
|
}
|
|
|
|
sign, err := encryptUserData(u, appg.Conf.Base.StoreEncryptKey)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
urlStr := fmt.Sprintf("%v?sign=%v", appg.Conf.URL.StoreUrl, url.QueryEscape(sign))
|
|
return urlStr
|
|
}
|
|
|
|
type UserData struct {
|
|
AppUid uint64 `json:"uid"` // uid
|
|
AppId int `json:"appId"`
|
|
Name string `json:"name"` // 姓名
|
|
Portrait string `json:"portrait"` // 头像
|
|
ExpireTime int64 `json:"expireTime"` // 失效时间的时间戳,单位秒
|
|
Balance int64 `json:"balance"`
|
|
}
|
|
|
|
func encryptUserData(u *UserData, key string) (sign string, err error) {
|
|
b, err := json.Marshal(u)
|
|
if err != nil {
|
|
return
|
|
}
|
|
encryptBytes, err := Encrypt(b, key)
|
|
if err != nil {
|
|
return
|
|
}
|
|
sign = string(encryptBytes)
|
|
return
|
|
}
|
|
|
|
// TODO 这里的别动
|
|
var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
|
|
|
|
func Encrypt(plainByte []byte, keyText string) (res string, err error) {
|
|
// 转换成字节数据, 方便加密
|
|
keyByte := []byte(keyText)
|
|
// 创建加密算法aes
|
|
c, err := aes.NewCipher(keyByte)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
//加密字符串
|
|
cfb := cipher.NewCFBEncrypter(c, commonIV)
|
|
cipherByte := make([]byte, len(plainByte))
|
|
cfb.XORKeyStream(cipherByte, plainByte)
|
|
res = base64.StdEncoding.EncodeToString(cipherByte)
|
|
return
|
|
}
|