Files
base-golang/pkg/crypto/interface.go
2026-02-07 16:03:15 +08:00

49 lines
1.5 KiB
Go
Raw Permalink 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 crypto
import "time"
// Encryptor 加密器接口
type Encryptor interface {
// Encrypt 加密数据
Encrypt(plaintext []byte) ([]byte, error)
// Decrypt 解密数据
Decrypt(ciphertext []byte) ([]byte, error)
// Name 返回加密算法名称
Name() string
}
// Signer 签名器接口
type Signer interface {
// Sign 生成签名
Sign(data []byte) ([]byte, error)
// Verify 验证签名
Verify(data, signature []byte) error
}
type Config struct {
SecretKey string `yaml:"secret_key" json:"secret_key"` // 对称加密密钥
SignKey string `yaml:"sign_key" json:"sign_key"` // 签名密钥
TimestampWindow time.Duration `yaml:"timestamp_window" json:"timestamp_window"` // 时间戳允许的时间窗口
EnableTimestamp bool `yaml:"enable_timestamp" json:"enable_timestamp"` // 是否启用时间戳验证
EnableSignature bool `yaml:"enable_signature" json:"enable_signature"` // 是否启用签名
}
type EncryptedRequest struct {
Data string `json:"data"` // 加密后的数据Base64
Signature string `json:"signature"` // 签名Base64
Timestamp int64 `json:"timestamp"` // 时间戳
RequestID string `json:"request_id"` // 请求ID
Algorithm string `json:"algorithm"` // 加密算法
}
// EncryptedResponse 加密响应体
type EncryptedResponse struct {
Data string `json:"data"`
Signature string `json:"signature"`
Timestamp int64 `json:"timestamp"`
RequestID string `json:"request_id"`
}