Some checks failed
Release / build-and-release (push) Failing after 1m31s
- Go 后端 (Gin + GORM + SQLite) 提供 API 和纯文本脚本服务 - Vite + React + TypeScript + Tailwind 前端 - 单二进制部署 (Go embed 前端静态文件) - Gitea Actions CI/CD: 打标签自动构建多平台 Release - 支持 bash/zsh/sh/fish/python3/node/ruby/php 8种运行环境 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
31 lines
619 B
Go
31 lines
619 B
Go
package idgen
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"math/big"
|
|
)
|
|
|
|
const idChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
func GenerateID(length int) (string, error) {
|
|
result := make([]byte, length)
|
|
for i := range result {
|
|
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(idChars))))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
result[i] = idChars[n.Int64()]
|
|
}
|
|
return string(result), nil
|
|
}
|
|
|
|
func GenerateToken() (string, error) {
|
|
token := make([]byte, 48)
|
|
_, err := rand.Read(token)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return base64.URLEncoding.EncodeToString(token), nil
|
|
}
|