Files
ddns-go/util/token.go
jeessy2 63b510bef2 Login page (#1094)
* feat: Add a login page

* feat: Modify save rules, more secure

* remove remoteAddr == "localhost"

* "登录失败次数过多,请等待 %d 分钟后再试

* cookie remove secure

* set cookie expires time by `NotAllowWanAccess`

* prettier

* fix: rename

* feat: auto login if unfilled

* feat: auto login if there is no username/password

* auto login if no username/password
2024-04-26 19:35:08 -07:00

32 lines
658 B
Go

package util
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"math/rand"
"time"
)
// GenerateToken 生成Token
func GenerateToken(username string) string {
key := []byte(generateRandomKey())
h := hmac.New(sha256.New, key)
msg := fmt.Sprintf("%s%d", username, time.Now().Unix())
h.Write([]byte(msg))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// generateRandomKey 生成随机密钥
func generateRandomKey() string {
// 设置随机种子
source := rand.NewSource(time.Now().UnixNano())
random := rand.New(source)
// 生成随机的64位整数
randomNumber := random.Uint64()
return fmt.Sprint(randomNumber)
}