- 后端 Go+Gin+GORM: 配置(OT_ env)/SQLite/Postgres 双驱动、用户体系(argon2id+JWT access/refresh)、 API Key(sk- 48位, 仅存 SHA-256 哈希) - 代理网关: /v1/chat/completions、/v1/responses、/v1/messages、/v1/models;错误按客户端协议返回 - 三协议互转(convert 包): Chat↔Messages↔Responses 请求/响应 + 流式 SSE 逐事件转换(直通优先) - 用量计费: 异步批量记账、余额扣减、balance_logs、usage_daily 日聚合 - 管理 API: 用户/渠道 CRUD+测试+模型导入/模型定价+绑定/统计/系统配置 - 前端 Vue3+TS+Tailwind(taste-skill 设计 tokens): Landing/登录注册/控制台/管理后台, 自建组件+Phosphor 图标+自建 SVG 趋势图, 已过 web-design-guidelines 复查 - mock 上游: OpenAI+Anthropic 双协议模拟(含流式) Co-Authored-By: Claude <noreply@anthropic.com>
127 lines
3.4 KiB
Go
127 lines
3.4 KiB
Go
// Package crypto 密码哈希(argon2id)与对称加密(AES-GCM)。
|
|
package crypto
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/argon2"
|
|
)
|
|
|
|
// PasswordHasher argon2id 参数(来自配置)。
|
|
type PasswordHasher struct {
|
|
Time uint32
|
|
Memory uint32
|
|
Threads uint8
|
|
KeyLen uint32
|
|
SaltLen int
|
|
}
|
|
|
|
func NewPasswordHasher(time, memory uint32, threads uint8, keyLen uint32, saltLen int) *PasswordHasher {
|
|
return &PasswordHasher{Time: time, Memory: memory, Threads: threads, KeyLen: keyLen, SaltLen: saltLen}
|
|
}
|
|
|
|
// HashPassword 编码为 $argon2id$v=19$m=...,t=...,p=...$salt$hash
|
|
func (h *PasswordHasher) HashPassword(password string) (string, error) {
|
|
salt := make([]byte, h.SaltLen)
|
|
if _, err := rand.Read(salt); err != nil {
|
|
return "", err
|
|
}
|
|
key := argon2.IDKey([]byte(password), salt, h.Time, h.Memory, h.Threads, h.KeyLen)
|
|
enc := base64.RawStdEncoding
|
|
return fmt.Sprintf("$argon2id$v=19$m=%d,t=%d,p=%d$%s$%s",
|
|
h.Memory, h.Time, h.Threads, enc.EncodeToString(salt), enc.EncodeToString(key)), nil
|
|
}
|
|
|
|
// VerifyPassword 校验密码,常数时间比较。
|
|
func (h *PasswordHasher) VerifyPassword(encoded, password string) (bool, error) {
|
|
parts := strings.Split(encoded, "$")
|
|
if len(parts) != 6 || parts[1] != "argon2id" {
|
|
return false, errors.New("invalid hash format")
|
|
}
|
|
var memory, time uint32
|
|
var threads uint8
|
|
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &time, &threads); err != nil {
|
|
return false, err
|
|
}
|
|
enc := base64.RawStdEncoding
|
|
salt, err := enc.DecodeString(parts[4])
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
want, err := enc.DecodeString(parts[5])
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
got := argon2.IDKey([]byte(password), salt, time, memory, threads, uint32(len(want)))
|
|
return subtle.ConstantTimeCompare(got, want) == 1, nil
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// AES-GCM 渠道密钥加密
|
|
|
|
// Encryptor 用主密钥加解密渠道上游 key。
|
|
type Encryptor struct {
|
|
key []byte
|
|
}
|
|
|
|
// NewEncryptor 主密钥必须为 16/24/32 字节;不足时用 SHA-256 派生固定 32 字节。
|
|
func NewEncryptor(master string) *Encryptor {
|
|
key := []byte(master)
|
|
switch len(key) {
|
|
case 16, 24, 32:
|
|
default:
|
|
key = sha256Sum(master)
|
|
}
|
|
return &Encryptor{key: key}
|
|
}
|
|
|
|
// Encrypt 输出 base64(nonce || ciphertext)
|
|
func (e *Encryptor) Encrypt(plain string) (string, error) {
|
|
block, err := aes.NewCipher(e.key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
if _, err := rand.Read(nonce); err != nil {
|
|
return "", err
|
|
}
|
|
ct := gcm.Seal(nil, nonce, []byte(plain), nil)
|
|
return base64.StdEncoding.EncodeToString(append(nonce, ct...)), nil
|
|
}
|
|
|
|
// Decrypt 解析 Encrypt 的输出。
|
|
func (e *Encryptor) Decrypt(enc string) (string, error) {
|
|
raw, err := base64.StdEncoding.DecodeString(enc)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
block, err := aes.NewCipher(e.key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(raw) < gcm.NonceSize() {
|
|
return "", errors.New("ciphertext too short")
|
|
}
|
|
nonce, ct := raw[:gcm.NonceSize()], raw[gcm.NonceSize():]
|
|
plain, err := gcm.Open(nil, nonce, ct, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(plain), nil
|
|
}
|