Reorganize project structure: - backend/cmd/openteam/ — entry point - backend/internal/ — core packages - backend/middleware/ — HTTP middleware - backend/router/ — route setup - backend/wire/ — dependency injection - backend/pkg/ — shared utilities - backend/go.mod, go.sum — Go module files Updated Makefile to work from backend/ directory. Removed old lowercase makefile.
31 lines
587 B
Go
31 lines
587 B
Go
package apikey
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"opencatd-open/internal/pkg/crypto"
|
|
"strings"
|
|
)
|
|
|
|
const Prefix = "sk-ot-"
|
|
|
|
// Generate 生成新的 API Key,返回明文和哈希
|
|
func Generate() (plaintext, hash string) {
|
|
b := make([]byte, 24)
|
|
_, _ = rand.Read(b)
|
|
raw := hex.EncodeToString(b)
|
|
plaintext = Prefix + raw
|
|
hash = crypto.Sha256Hex(plaintext)
|
|
return
|
|
}
|
|
|
|
// Valid 校验 API Key 格式
|
|
func Valid(key string) bool {
|
|
return strings.HasPrefix(key, Prefix)
|
|
}
|
|
|
|
// Hash 计算 API Key 的 SHA-256 哈希
|
|
func Hash(key string) string {
|
|
return crypto.Sha256Hex(key)
|
|
}
|