-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
275 lines (213 loc) · 7.37 KB
/
examples.py
File metadata and controls
275 lines (213 loc) · 7.37 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
隐私信息储存箱智能体使用示例
演示如何通过Python代码使用智能体的各项功能
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from core.agent_engine import AgentEngine
def example_basic_usage():
"""
基本使用示例
"""
print("=" * 60)
print("示例1: 基本使用流程")
print("=" * 60)
agent = AgentEngine()
init_result = agent.initialize()
print(f"初始化结果: {init_result['status']}")
register_result = agent.register_user(
username="alice",
password="SecurePass@123",
roles=["user"],
enable_mfa=False
)
print(f"用户注册: {register_result}")
login_result = agent.login("alice", "SecurePass@123")
print(f"用户登录: {login_result['status']}")
if login_result['status'] == 'success':
store_result = agent.store_data(
name="我的密码",
data="my_secret_password_123",
tags=["密码", "重要"],
encrypt=True
)
print(f"存储数据: {store_result}")
retrieve_result = agent.retrieve_data(name="我的密码")
print(f"检索数据: {retrieve_result}")
agent.logout()
agent.shutdown()
def example_store_sensitive_info():
"""
存储敏感信息示例
"""
print("\n" + "=" * 60)
print("示例2: 存储各类敏感信息")
print("=" * 60)
agent = AgentEngine()
agent.initialize()
agent.register_user("bob", "Bob@456")
agent.login("bob", "Bob@456")
bank_card = {
"card_type": "信用卡",
"number": "6222 **** **** 8888",
"cvv": "***",
"expiry": "12/2028",
"bank": "中国银行"
}
agent.store_data("银行卡信息", bank_card, tags=["金融", "银行卡"])
id_card = {
"type": "身份证",
"number": "110************1234",
"name": "张三",
"issue_date": "2020-01-01"
}
agent.store_data("身份证信息", id_card, tags=["证件", "重要"])
wifi_password = {
"ssid": "MyHomeWiFi",
"password": "w1f1_p@ssw0rd",
"security": "WPA3"
}
agent.store_data("家庭WiFi", wifi_password, tags=["网络", "密码"])
crypto_wallet = {
"type": "比特币钱包",
"address": "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh",
"seed_phrase": "word1 word2 word3 ... word12"
}
agent.store_data("加密货币钱包", crypto_wallet, tags=["金融", "加密货币"])
search_result = agent.search_data(tags=["金融"])
print(f"\n金融相关数据: {search_result['count']} 条")
for item in search_result['results']:
print(f" - {item['name']}")
agent.logout()
agent.shutdown()
def example_quantum_crypto():
"""
量子密码功能示例
"""
print("\n" + "=" * 60)
print("示例3: 量子密码功能")
print("=" * 60)
agent = AgentEngine()
init_result = agent.initialize()
print(f"\nQKD状态: {init_result['crypto']['qkd_stats']}")
print(f"密钥哈希: {init_result['crypto']['master_key_hash']}")
agent.register_user("crypto_user", "Crypto@789")
agent.login("crypto_user", "Crypto@789")
agent.store_data("测试数据", "这是一条测试数据")
print("\n执行密钥轮换...")
rotate_result = agent.rotate_keys()
print(f"轮换结果: {rotate_result}")
retrieve_result = agent.retrieve_data(name="测试数据")
print(f"轮换后数据检索: {retrieve_result['status']}")
agent.logout()
agent.shutdown()
def example_access_control():
"""
访问控制示例
"""
print("\n" + "=" * 60)
print("示例4: 访问控制和角色管理")
print("=" * 60)
agent = AgentEngine()
agent.initialize()
agent.register_user("admin_user", "Admin@123", roles=["admin"])
agent.register_user("viewer_user", "Viewer@123", roles=["viewer"])
agent.login("admin_user", "Admin@123")
agent.store_data("管理员数据", "只有管理员能看到", tags=["机密"])
agent.logout()
agent.login("viewer_user", "Viewer@123")
search_result = agent.search_data()
print(f"viewer用户可见数据: {search_result['count']} 条")
agent.logout()
agent.shutdown()
def example_audit_logging():
"""
审计日志示例
"""
print("\n" + "=" * 60)
print("示例5: 审计日志")
print("=" * 60)
agent = AgentEngine()
agent.initialize()
agent.register_user("audit_user", "Audit@123")
agent.login("audit_user", "Audit@123")
for i in range(3):
agent.store_data(f"数据_{i}", f"内容_{i}")
audit_result = agent.get_audit_log()
print(f"\n审计日志条数: {audit_result['count']}")
event_types = {}
for event in audit_result['events']:
et = event['event_type']
event_types[et] = event_types.get(et, 0) + 1
print("事件统计:")
for et, count in event_types.items():
print(f" - {et}: {count} 次")
agent.logout()
agent.shutdown()
def example_api_usage():
"""
API使用示例 (需要先启动API服务)
"""
print("\n" + "=" * 60)
print("示例6: 通过API使用智能体")
print("=" * 60)
import requests
import json
base_url = "http://127.0.0.1:8443"
try:
response = requests.get(f"{base_url}/")
print(f"API状态: {response.json()}")
register_data = {
"username": "api_user",
"password": "ApiUser@123",
"roles": ["user"],
"enable_mfa": False
}
response = requests.post(
f"{base_url}/api/auth/register",
json=register_data
)
print(f"注册结果: {response.json()}")
login_data = {
"username": "api_user",
"password": "ApiUser@123"
}
response = requests.post(
f"{base_url}/api/auth/login",
json=login_data
)
login_result = response.json()
print(f"登录结果: {login_result}")
if login_result['status'] == 'success':
token = login_result['session_id']
headers = {"Authorization": f"Bearer {token}"}
store_data = {
"name": "API存储的数据",
"data": {"key": "value"},
"tags": ["api", "test"],
"encrypt": True
}
response = requests.post(
f"{base_url}/api/data/store",
json=store_data,
headers=headers
)
print(f"存储结果: {response.json()}")
response = requests.post(
f"{base_url}/api/auth/logout",
headers=headers
)
print(f"登出结果: {response.json()}")
except requests.exceptions.ConnectionError:
print("无法连接到API服务,请先运行: python main.py --mode api")
if __name__ == "__main__":
print("\n隐私信息储存箱智能体 - 使用示例\n")
example_basic_usage()
example_store_sensitive_info()
example_quantum_crypto()
example_access_control()
example_audit_logging()
print("\n" + "=" * 60)
print("所有示例执行完成!")
print("=" * 60)