移除字段加密硬编码主密钥,改为 FIELD_ENCRYPTION_KEY 注入。 保留 FIELD_ENCRYPTION_LEGACY_KEY 透明回退旧密文,新写入统一使用主密钥;生产环境校验主密钥和 legacy 密钥长度、占位符及相等关系,并统一生产环境判断口径。 补充配置与旧密文兼容回归测试。
146 lines
4.6 KiB
Go
146 lines
4.6 KiB
Go
package crypto
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
testPrimaryKey = "0123456789abcdef0123456789abcdef" // 32 字节
|
|
testLegacyKey = "hfb-sys-2024-secret-key-32bytes!"
|
|
)
|
|
|
|
func TestFieldEncryptorRoundTrip(t *testing.T) {
|
|
enc, err := NewFieldEncryptor(testPrimaryKey)
|
|
if err != nil {
|
|
t.Fatalf("NewFieldEncryptor() error = %v", err)
|
|
}
|
|
cases := []string{"", "张三", "110101199003077734", "6222021234567890", "with spaces and 中文"}
|
|
for _, plain := range cases {
|
|
cipher, err := enc.Encrypt(plain)
|
|
if err != nil {
|
|
t.Fatalf("Encrypt(%q) error = %v", plain, err)
|
|
}
|
|
got, err := enc.Decrypt(cipher)
|
|
if err != nil {
|
|
t.Fatalf("Decrypt() error = %v", err)
|
|
}
|
|
if got != plain {
|
|
t.Fatalf("round-trip mismatch: got %q, want %q", got, plain)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFieldEncryptorEmptyStringPassthrough(t *testing.T) {
|
|
enc, _ := NewFieldEncryptor(testPrimaryKey)
|
|
cipher, err := enc.Encrypt("")
|
|
if err != nil {
|
|
t.Fatalf("Encrypt(\"\") error = %v", err)
|
|
}
|
|
if cipher != "" {
|
|
t.Fatalf("Encrypt(\"\") = %q, want empty", cipher)
|
|
}
|
|
got, err := enc.Decrypt("")
|
|
if err != nil {
|
|
t.Fatalf("Decrypt(\"\") error = %v", err)
|
|
}
|
|
if got != "" {
|
|
t.Fatalf("Decrypt(\"\") = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
// TestFieldEncryptorLegacyFallback 验证旧密钥加密的密文,新 FieldEncryptor(含 legacy)能解出。
|
|
func TestFieldEncryptorLegacyFallback(t *testing.T) {
|
|
legacyEnc, _ := NewFieldEncryptor(testLegacyKey)
|
|
plain := "110101199003077734"
|
|
cipher, err := legacyEnc.Encrypt(plain)
|
|
if err != nil {
|
|
t.Fatalf("legacy Encrypt() error = %v", err)
|
|
}
|
|
|
|
// 新加密器:primary 不同,legacy 含旧密钥
|
|
primaryEnc, err := NewFieldEncryptor(testPrimaryKey, testLegacyKey)
|
|
if err != nil {
|
|
t.Fatalf("NewFieldEncryptor(primary, legacy) error = %v", err)
|
|
}
|
|
got, err := primaryEnc.Decrypt(cipher)
|
|
if err != nil {
|
|
t.Fatalf("Decrypt() with legacy fallback error = %v", err)
|
|
}
|
|
if got != plain {
|
|
t.Fatalf("legacy fallback mismatch: got %q, want %q", got, plain)
|
|
}
|
|
}
|
|
|
|
// TestFieldEncryptorNoLegacyFailsOnOldCiphertext 验证无 legacy 时解旧密文失败。
|
|
func TestFieldEncryptorNoLegacyFailsOnOldCiphertext(t *testing.T) {
|
|
legacyEnc, _ := NewFieldEncryptor(testLegacyKey)
|
|
cipher, _ := legacyEnc.Encrypt("secret")
|
|
|
|
primaryOnly, _ := NewFieldEncryptor(testPrimaryKey)
|
|
if _, err := primaryOnly.Decrypt(cipher); err == nil {
|
|
t.Fatal("Decrypt() with no legacy should fail on old ciphertext")
|
|
}
|
|
}
|
|
|
|
// TestFieldEncryptorDecryptGarbageFails 验证非密文输入解密失败。
|
|
func TestFieldEncryptorDecryptGarbageFails(t *testing.T) {
|
|
enc, _ := NewFieldEncryptor(testPrimaryKey)
|
|
cases := []string{"not-base64!!!", "dG9vIHNob3J0"} // 非 base64 / 解码后太短
|
|
for _, c := range cases {
|
|
if _, err := enc.Decrypt(c); err == nil {
|
|
t.Fatalf("Decrypt(%q) should fail", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewFieldEncryptorInvalidKeyLength(t *testing.T) {
|
|
// AES 只接受 16/24/32 字节,其余长度必须失败
|
|
cases := []int{0, 1, 15, 17, 23, 25, 31, 33, 40}
|
|
for _, n := range cases {
|
|
key := string(make([]byte, n)) // n 个零字节
|
|
if _, err := NewFieldEncryptor(key); err == nil {
|
|
t.Fatalf("NewFieldEncryptor(len=%d) should fail on invalid length", n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestNewFieldEncryptorDedupsLegacyKey 验证 legacy 等于 primary 时被忽略(避免无效回退)。
|
|
func TestNewFieldEncryptorDedupsLegacyKey(t *testing.T) {
|
|
enc, err := NewFieldEncryptor(testPrimaryKey, testPrimaryKey, "")
|
|
if err != nil {
|
|
t.Fatalf("NewFieldEncryptor() error = %v", err)
|
|
}
|
|
if len(enc.legacy) != 0 {
|
|
t.Fatalf("legacy should be empty after dedup, got %d", len(enc.legacy))
|
|
}
|
|
}
|
|
|
|
func TestMockEncryptorPassthrough(t *testing.T) {
|
|
m := &MockEncryptor{}
|
|
plain := "明文直通"
|
|
cipher, err := m.Encrypt(plain)
|
|
if err != nil || cipher != plain {
|
|
t.Fatalf("MockEncryptor.Encrypt(%q) = (%q,%v), want (%q,nil)", plain, cipher, err, plain)
|
|
}
|
|
got, err := m.Decrypt(cipher)
|
|
if err != nil || got != plain {
|
|
t.Fatalf("MockEncryptor.Decrypt(%q) = (%q,%v), want (%q,nil)", cipher, got, err, plain)
|
|
}
|
|
}
|
|
|
|
// TestFieldEncryptorCipherFormatStable 验证密文是 base64(nonce||ct||tag),格式与历史硬编码实现一致。
|
|
// 确保迁移后存量密文(旧实现加密)能被新实现的 Decrypt 正确解析。
|
|
func TestFieldEncryptorCipherFormatStable(t *testing.T) {
|
|
enc, _ := NewFieldEncryptor(testLegacyKey)
|
|
cipher, _ := enc.Encrypt("test")
|
|
// base64 解码后长度 > nonce(12) + tag(16) = 28,且明文长度 4 → 总长 32
|
|
decoded, err := base64.StdEncoding.DecodeString(cipher)
|
|
if err != nil {
|
|
t.Fatalf("cipher not valid base64: %v", err)
|
|
}
|
|
if len(decoded) <= 28 {
|
|
t.Fatalf("decoded length = %d, want > 28 (nonce+tag)", len(decoded))
|
|
}
|
|
}
|