Files
ycc-proxy-server/pkg/captcha/encrypt_scene.go
2026-02-25 16:38:58 +08:00

37 lines
1020 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package captcha
import (
"encoding/base64"
"fmt"
"time"
"ycc-server/pkg/lzkit/crypto"
)
// GenerateEncryptedSceneID 生成加密的场景ID
// 格式: sceneId&timestamp&expireTime -> AES-256-CBC + PKCS7 -> Base64(IV + ciphertext)
func GenerateEncryptedSceneID(sceneId, ekey string, expireSeconds int) (string, error) {
// 参数校验
if expireSeconds <= 0 || expireSeconds > 86400 {
expireSeconds = 3600 // 默认1小时
}
// 构建明文: sceneId&timestamp&expireTime
ts := time.Now().Unix()
plaintext := fmt.Sprintf("%s&%d&%d", sceneId, ts, expireSeconds)
// 解码 ekeyBase64 -> 32字节密钥
keyBytes, err := base64.StdEncoding.DecodeString(ekey)
if err != nil {
return "", fmt.Errorf("decode ekey error: %w", err)
}
// 验证密钥长度AES-256 需要 32 字节)
if len(keyBytes) != 32 {
return "", fmt.Errorf("invalid ekey length, need 32 bytes after base64 decode, got %d", len(keyBytes))
}
// 使用 AES 加密
return crypto.AesEncrypt([]byte(plaintext), keyBytes)
}