添加 config/utils.go
This commit is contained in:
35
config/utils.go
Normal file
35
config/utils.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// GenerateRandomString 生成加密安全的随机字符串
|
||||
func GenerateRandomString(length int) string {
|
||||
bytes := make([]byte, length)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
panic("failed to generate random string: " + err.Error())
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
// GenerateRandomBase64 生成 Base64 编码的随机字符串
|
||||
func GenerateRandomBase64(length int) string {
|
||||
bytes := make([]byte, length)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
panic("failed to generate random string: " + err.Error())
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
// GenerateSessionSecret 生成会话密钥
|
||||
func GenerateSessionSecret() string {
|
||||
return GenerateRandomString(32) // 64 个十六进制字符
|
||||
}
|
||||
|
||||
// GenerateCSRFToken 生成 CSRF 令牌
|
||||
func GenerateCSRFToken() string {
|
||||
return GenerateRandomBase64(32)
|
||||
}
|
||||
Reference in New Issue
Block a user