61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package ysqr
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"91porn-server/common/crypt"
|
|
)
|
|
|
|
type ActType string
|
|
|
|
const (
|
|
Login ActType = "||"
|
|
|
|
PFV = "pfv"
|
|
)
|
|
|
|
type LoginClaims struct {
|
|
DevID string `json:"devID"`
|
|
T ActType `json:"t"`
|
|
}
|
|
|
|
func sign(devID string, t ActType, secret string) string {
|
|
j, _ := json.Marshal(LoginClaims{
|
|
DevID: devID,
|
|
T: t,
|
|
})
|
|
sha256 := crypt.StrToHmacSha256(string(j), secret)
|
|
if len(sha256) > 24 {
|
|
return sha256[8:24]
|
|
}
|
|
return sha256
|
|
}
|
|
|
|
type Content struct {
|
|
UID uint64
|
|
T ActType
|
|
LoginClaims
|
|
}
|
|
|
|
// String pfv2505938||********
|
|
func (c Content) String(secret string) string {
|
|
s := sign(c.DevID, c.T, secret)
|
|
return fmt.Sprintf("%s%d%s%s", PFV, c.UID, string(c.T), s)
|
|
}
|
|
|
|
func GetUIDFromQrCnt(t ActType, qrCnt string) (uint64, error) {
|
|
list := strings.Split(qrCnt, string(t))
|
|
if len(list) != 2 {
|
|
return 0, ErrContentInvalid{}
|
|
}
|
|
uidstr := strings.ReplaceAll(list[0], PFV, "")
|
|
uid, err := strconv.ParseUint(uidstr, 10, 64)
|
|
if err != nil {
|
|
return 0, ErrContentInvalid{}
|
|
}
|
|
return uid, nil
|
|
}
|