55use capsula_key:: { Key , KeySign } ;
66use time:: OffsetDateTime ;
77
8- use crate :: encapsulator:: CapsulaBuilder ;
9- use crate :: decapsulator:: { CapsuleDecryptor , DecapsulationResult } ;
10- use crate :: error:: { CoreError , Result } ;
11- use crate :: protocol:: capsule:: Capsula1 ;
12- use crate :: protocol:: types:: CapsulaGranted ;
8+ use crate :: {
9+ decapsulator:: { CapsuleDecryptor , DecapsulationResult } ,
10+ encapsulator:: CapsulaBuilder ,
11+ error:: { CoreError , Result } ,
12+ protocol:: { capsule:: Capsula1 , types:: CapsulaGranted } ,
13+ } ;
1314
1415/// 数据胶囊API
1516pub struct CapsulaApi ;
1617
1718impl CapsulaApi {
1819 /// 简单封包操作
19- ///
20+ ///
2021 /// # 参数
2122 /// * `data` - 原始数据
2223 /// * `data_type` - 数据类型(如 "medical.blood_test")
@@ -73,7 +74,7 @@ impl CapsulaApi {
7374 }
7475
7576 /// 简单解包操作(RSA密钥)
76- ///
77+ ///
7778 /// # 参数
7879 /// * `capsule` - 数据胶囊
7980 /// * `private_key` - 接收者的RSA私钥
@@ -86,7 +87,7 @@ impl CapsulaApi {
8687 producer_public_key : Option < Vec < u8 > > ,
8788 ) -> Result < DecapsulationResult > {
8889 let mut decryptor = CapsuleDecryptor :: new_rsa ( private_key, user_id) ;
89-
90+
9091 if let Some ( pub_key) = producer_public_key {
9192 decryptor = decryptor. with_producer_public_key ( pub_key) ;
9293 }
@@ -95,7 +96,7 @@ impl CapsulaApi {
9596 }
9697
9798 /// 简单解包操作(P256密钥)
98- ///
99+ ///
99100 /// # 参数
100101 /// * `capsule` - 数据胶囊
101102 /// * `private_key` - 接收者的P256私钥
@@ -108,7 +109,7 @@ impl CapsulaApi {
108109 producer_public_key : Option < Vec < u8 > > ,
109110 ) -> Result < DecapsulationResult > {
110111 let mut decryptor = CapsuleDecryptor :: new_p256 ( private_key, user_id) ;
111-
112+
112113 if let Some ( pub_key) = producer_public_key {
113114 decryptor = decryptor. with_producer_public_key ( pub_key) ;
114115 }
@@ -125,7 +126,7 @@ impl CapsulaApi {
125126 // 创建一个临时的虚拟密钥用于验证
126127 let dummy_key = capsula_key:: RsaKey :: generate_2048 ( )
127128 . map_err ( |e| CoreError :: Other ( format ! ( "Failed to create dummy key: {}" , e) ) ) ?;
128-
129+
129130 let decryptor = if let Some ( pub_key) = producer_public_key {
130131 CapsuleDecryptor :: new_rsa ( dummy_key, user_id. unwrap_or_default ( ) )
131132 . with_producer_public_key ( pub_key)
@@ -135,9 +136,9 @@ impl CapsulaApi {
135136
136137 // 只进行结构和签名验证,不解密
137138 match decryptor. decapsulate ( capsule) {
138- Ok ( result) => Ok ( result. verification . signature_valid &&
139- result. verification . policy_valid &&
140- result. verification . time_valid ) ,
139+ Ok ( result) => Ok ( result. verification . signature_valid
140+ && result. verification . policy_valid
141+ && result. verification . time_valid ) ,
141142 Err ( _) => Ok ( false ) ,
142143 }
143144 }
@@ -154,9 +155,8 @@ pub fn create_medical_capsule<S: KeySign>(
154155 expires_in_days : Option < u64 > ,
155156) -> Result < Capsula1 > {
156157 let data_type = format ! ( "medical.{}" , report_type) ;
157- let expires_at = expires_in_days. map ( |days| {
158- OffsetDateTime :: now_utc ( ) + time:: Duration :: days ( days as i64 )
159- } ) ;
158+ let expires_at =
159+ expires_in_days. map ( |days| OffsetDateTime :: now_utc ( ) + time:: Duration :: days ( days as i64 ) ) ;
160160
161161 CapsulaApi :: encapsulate_with_policy (
162162 medical_data,
@@ -187,15 +187,16 @@ pub fn decrypt_medical_capsule_rsa(
187187
188188#[ cfg( test) ]
189189mod tests {
190+ use capsula_key:: { Key , RsaKey } ;
191+
190192 use super :: * ;
191- use capsula_key:: { RsaKey , Key , KeySign } ;
192193
193194 #[ test]
194195 fn test_simple_encapsulation ( ) {
195196 let data = b"Test medical report" . to_vec ( ) ;
196197 let producer_key = RsaKey :: generate_2048 ( ) . unwrap ( ) ;
197198 let recipient_key = RsaKey :: generate_2048 ( ) . unwrap ( ) ;
198-
199+
199200 let recipients = vec ! [ ( "user1" . to_string( ) , & recipient_key as & dyn Key ) ] ;
200201
201202 let result = CapsulaApi :: encapsulate_simple (
@@ -219,7 +220,7 @@ mod tests {
219220 let medical_data = b"Blood test results: Normal" . to_vec ( ) ;
220221 let doctor_key = RsaKey :: generate_2048 ( ) . unwrap ( ) ;
221222 let nurse_key = RsaKey :: generate_2048 ( ) . unwrap ( ) ;
222-
223+
223224 let authorized_users = vec ! [ ( "nurse1" . to_string( ) , & nurse_key as & dyn Key ) ] ;
224225
225226 let result = create_medical_capsule (
@@ -253,7 +254,8 @@ mod tests {
253254 "Owner" . to_string ( ) ,
254255 & producer_key,
255256 & recipients,
256- ) . unwrap ( ) ;
257+ )
258+ . unwrap ( ) ;
257259
258260 // TODO: 需要实现公钥导出功能才能完整测试签名验证
259261 let verification_result = CapsulaApi :: verify_capsule (
@@ -264,4 +266,4 @@ mod tests {
264266
265267 assert ! ( verification_result. is_ok( ) ) ;
266268 }
267- }
269+ }
0 commit comments