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) }