|
- package youlianghui
-
- import (
- "crypto/sha1"
- "encoding/base64"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
-
- // GetToken 获取token
- func GetToken(memberId, secret string) (token string) {
- timestamp := strconv.FormatInt(time.Now().Unix(), 10) // 时间戳,精确到秒
- // 计算签名
- hash := sha1.New()
- hash.Write([]byte(memberId + secret + timestamp))
- sign := fmt.Sprintf("%x", hash.Sum(nil))
-
- // 构建列表
- listV := []string{memberId, timestamp, sign}
- plain := strings.Join(listV, ",")
-
- // 编码为Base64
- token = base64.StdEncoding.EncodeToString([]byte(plain))
-
- return
- }
|