Files
mayfly-go/server/internal/pkg/config/aes.go
2026-01-25 14:16:16 +08:00

35 lines
715 B
Go

package config
import (
"fmt"
"mayfly-go/pkg/starter"
"mayfly-go/pkg/utils/cryptox"
)
type Aes struct {
Key string `yaml:"key"`
}
// 编码并base64
func (a *Aes) EncryptBase64(data []byte) (string, error) {
return cryptox.AesEncryptBase64(data, []byte(a.Key))
}
// base64解码后再aes解码
func (a *Aes) DecryptBase64(data string) ([]byte, error) {
return cryptox.AesDecryptBase64(data, []byte(a.Key))
}
func (a *Aes) ApplyDefaults() error {
if a.Key == "" {
return nil
}
aesKeyLen := len(a.Key)
if aesKeyLen != 16 && aesKeyLen != 24 && aesKeyLen != 32 {
return starter.NewConfigError("aes.key", fmt.Sprintf("长度需为16、24、32位长度, 当前为%d位", aesKeyLen))
}
return nil
}