-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_encryption.py
More file actions
79 lines (66 loc) · 2.16 KB
/
test_encryption.py
File metadata and controls
79 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""测试加密功能"""
import sys
import os
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app.config import get_settings
from app.utils.crypto import init_encryptor, encrypt_credentials, decrypt_credentials
def test_encryption():
"""测试加密功能"""
print("=" * 60)
print("测试加密功能")
print("=" * 60)
# 1. 加载配置
print("\n1. 加载配置...")
settings = get_settings()
print(f" ENCRYPTION_KEY: {settings.encryption_key[:20]}..." if settings.encryption_key else " ENCRYPTION_KEY: (未配置)")
if not settings.encryption_key:
print(" ❌ ENCRYPTION_KEY 未配置")
return False
# 2. 初始化加密器
print("\n2. 初始化加密器...")
try:
init_encryptor(settings.encryption_key)
print(" ✅ 加密器初始化成功")
except Exception as e:
print(f" ❌ 加密器初始化失败: {e}")
return False
# 3. 测试加密
print("\n3. 测试加密...")
test_data = {
"api_key": "test-key-123",
"secret": "test-secret"
}
print(f" 原始数据: {test_data}")
try:
encrypted = encrypt_credentials(test_data)
print(f" 加密后: {encrypted[:50]}...")
print(" ✅ 加密成功")
except Exception as e:
print(f" ❌ 加密失败: {e}")
import traceback
traceback.print_exc()
return False
# 4. 测试解密
print("\n4. 测试解密...")
try:
decrypted = decrypt_credentials(encrypted)
print(f" 解密后: {decrypted}")
if decrypted == test_data:
print(" ✅ 解密成功,数据一致")
else:
print(" ❌ 解密后数据不一致")
return False
except Exception as e:
print(f" ❌ 解密失败: {e}")
import traceback
traceback.print_exc()
return False
print("\n" + "=" * 60)
print("✅ 所有测试通过")
print("=" * 60)
return True
if __name__ == "__main__":
success = test_encryption()
sys.exit(0 if success else 1)