Files
rag-local/common/util.go
T
2026-08-05 10:28:44 +08:00

23 lines
534 B
Go

package common
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
)
// RandomToken 生成 n 字节随机数的十六进制字符串(2n 位)
func RandomToken(n int) string {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
panic("crypto/rand failed: " + err.Error())
}
return hex.EncodeToString(b)
}
// TokenFingerprint 计算令牌 SHA-256 指纹前 16 位,用于 JWT 会话校验
func TokenFingerprint(token string) string {
sum := sha256.Sum256([]byte(token))
return hex.EncodeToString(sum[:8])
}