ciphern 0.2.1

Enterprise-grade cryptographic library
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright (c) 2025 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

use crate::error::CryptoError;
use crate::i18n::translate;
use chrono::{DateTime, Utc};
use sha2::Digest;
use sha2::Sha256;
use std::collections::HashMap;
use uuid::Uuid;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum PqcAlgorithm {
    Kyber512,
    Kyber768,
    Kyber1024,
    Dilithium2,
    Dilithium3,
    Dilithium5,
    Falcon512,
    Falcon1024,
    SphincsSha256128f,
    SphincsSha256192f,
    SphincsSha256256f,
}

impl PqcAlgorithm {
    pub fn key_size(&self) -> usize {
        match self {
            PqcAlgorithm::Kyber512 => 800,
            PqcAlgorithm::Kyber768 => 1184,
            PqcAlgorithm::Kyber1024 => 1568,
            PqcAlgorithm::Dilithium2 => 2528,
            PqcAlgorithm::Dilithium3 => 4000,
            PqcAlgorithm::Dilithium5 => 4864,
            PqcAlgorithm::Falcon512 => 897,
            PqcAlgorithm::Falcon1024 => 1769,
            PqcAlgorithm::SphincsSha256128f => 32,
            PqcAlgorithm::SphincsSha256192f => 48,
            PqcAlgorithm::SphincsSha256256f => 64,
        }
    }

    #[allow(dead_code)]
    pub fn name(&self) -> &'static str {
        match self {
            PqcAlgorithm::Kyber512 => "Kyber512",
            PqcAlgorithm::Kyber768 => "Kyber768",
            PqcAlgorithm::Kyber1024 => "Kyber1024",
            PqcAlgorithm::Dilithium2 => "Dilithium2",
            PqcAlgorithm::Dilithium3 => "Dilithium3",
            PqcAlgorithm::Dilithium5 => "Dilithium5",
            PqcAlgorithm::Falcon512 => "Falcon512",
            PqcAlgorithm::Falcon1024 => "Falcon1024",
            PqcAlgorithm::SphincsSha256128f => "SPHINCS+ SHA-256-128f",
            PqcAlgorithm::SphincsSha256192f => "SPHINCS+ SHA-256-192f",
            PqcAlgorithm::SphincsSha256256f => "SPHINCS+ SHA-256-256f",
        }
    }

    #[allow(dead_code)]
    pub fn security_level(&self) -> &'static str {
        match self {
            PqcAlgorithm::Kyber512 => "NIST Level 1",
            PqcAlgorithm::Kyber768 => "NIST Level 3",
            PqcAlgorithm::Kyber1024 => "NIST Level 5",
            PqcAlgorithm::Dilithium2 => "NIST Level 2",
            PqcAlgorithm::Dilithium3 => "NIST Level 3",
            PqcAlgorithm::Dilithium5 => "NIST Level 5",
            PqcAlgorithm::Falcon512 => "NIST Level 1",
            PqcAlgorithm::Falcon1024 => "NIST Level 5",
            PqcAlgorithm::SphincsSha256128f => "NIST Level 1",
            PqcAlgorithm::SphincsSha256192f => "NIST Level 3",
            PqcAlgorithm::SphincsSha256256f => "NIST Level 5",
        }
    }
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum PqcKey {
    KyberPublic(Vec<u8>),
    KyberSecret(Vec<u8>),
    KyberCiphertext(Vec<u8>),
    DilithiumPublic(Vec<u8>),
    DilithiumSecret(Vec<u8>),
    FalconPublic(Vec<u8>),
    FalconSecret(Vec<u8>),
    SphincsPublic(Vec<u8>),
    SphincsSecret(Vec<u8>),
    ClassicMceliecePublic(Vec<u8>),
    ClassicMcelieceSecret(Vec<u8>),
    HybridKey(Vec<u8>),
}

impl PqcKey {
    #[allow(dead_code)]
    pub fn size(&self) -> usize {
        match self {
            PqcKey::KyberPublic(v) => v.len(),
            PqcKey::KyberSecret(v) => v.len(),
            PqcKey::KyberCiphertext(v) => v.len(),
            PqcKey::DilithiumPublic(v) => v.len(),
            PqcKey::DilithiumSecret(v) => v.len(),
            PqcKey::FalconPublic(v) => v.len(),
            PqcKey::FalconSecret(v) => v.len(),
            PqcKey::SphincsPublic(v) => v.len(),
            PqcKey::SphincsSecret(v) => v.len(),
            PqcKey::ClassicMceliecePublic(v) => v.len(),
            PqcKey::ClassicMcelieceSecret(v) => v.len(),
            PqcKey::HybridKey(v) => v.len(),
        }
    }
}

#[allow(dead_code)]
pub trait PqcOperations {
    #[allow(dead_code)]
    fn generate_keypair(&self, _algorithm: PqcAlgorithm)
        -> Result<(Vec<u8>, Vec<u8>), CryptoError>;
    #[allow(dead_code)]
    fn encapsulate(
        &self,
        _public_key: &[u8],
        _algorithm: PqcAlgorithm,
    ) -> Result<(Vec<u8>, Vec<u8>), CryptoError>;
    #[allow(dead_code)]
    fn decapsulate(
        &self,
        _secret_key: &[u8],
        _ciphertext: &[u8],
        _algorithm: PqcAlgorithm,
    ) -> Result<Vec<u8>, CryptoError>;
    #[allow(dead_code)]
    fn sign(
        &self,
        _secret_key: &[u8],
        _message: &[u8],
        _algorithm: PqcAlgorithm,
    ) -> Result<Vec<u8>, CryptoError>;
    #[allow(dead_code)]
    fn verify(
        &self,
        _public_key: &[u8],
        _message: &[u8],
        _signature: &[u8],
        _algorithm: PqcAlgorithm,
    ) -> Result<bool, CryptoError>;
}

#[allow(dead_code)]
pub struct PqcKeyManager {
    keys: HashMap<String, PqcKeyEntry>,
    algorithm: PqcAlgorithm,
}

#[allow(dead_code)]
struct PqcKeyEntry {
    key: PqcKey,
    created_at: DateTime<Utc>,
    metadata: HashMap<String, String>,
}

#[allow(dead_code)]
impl PqcKeyManager {
    pub fn new(algorithm: PqcAlgorithm) -> Result<Self, CryptoError> {
        Ok(Self {
            keys: HashMap::new(),
            algorithm,
        })
    }

    #[allow(dead_code)]
    pub fn generate_keypair(&mut self) -> Result<(String, String), CryptoError> {
        let public_key_id = Uuid::new_v4().to_string();
        let secret_key_id = Uuid::new_v4().to_string();

        let key_size = self.algorithm.key_size();
        let mut public_key = vec![0u8; key_size];
        let mut secret_key = vec![0u8; key_size];

        Self::generate_fake_keypair(&mut public_key, &mut secret_key, self.algorithm)?;

        self.keys.insert(
            public_key_id.clone(),
            PqcKeyEntry {
                key: PqcKey::KyberPublic(public_key.clone()),
                created_at: Utc::now(),
                metadata: HashMap::new(),
            },
        );

        self.keys.insert(
            secret_key_id.clone(),
            PqcKeyEntry {
                key: PqcKey::KyberSecret(secret_key.clone()),
                created_at: Utc::now(),
                metadata: HashMap::new(),
            },
        );

        Ok((public_key_id, secret_key_id))
    }

    fn generate_fake_keypair(
        public_key: &mut [u8],
        secret_key: &mut [u8],
        _algorithm: PqcAlgorithm,
    ) -> Result<(), CryptoError> {
        let mut rng = rand::thread_rng();
        use rand::RngCore;

        rng.fill_bytes(public_key);
        rng.fill_bytes(secret_key);

        Ok(())
    }

    #[allow(dead_code)]
    pub fn encapsulate(&mut self, public_key_id: &str) -> Result<(String, Vec<u8>), CryptoError> {
        let ciphertext_id = Uuid::new_v4().to_string();

        let entry = self
            .keys
            .get(public_key_id)
            .ok_or_else(|| CryptoError::KeyNotFound(public_key_id.to_string()))?;

        match &entry.key {
            PqcKey::KyberPublic(_public_key) => {
                let mut shared_secret = vec![0u8; 32];
                let mut ciphertext = vec![0u8; self.algorithm.key_size()];

                let mut rng = rand::thread_rng();
                use rand::RngCore;
                rng.fill_bytes(&mut shared_secret);
                rng.fill_bytes(&mut ciphertext);

                self.keys.insert(
                    ciphertext_id.clone(),
                    PqcKeyEntry {
                        key: PqcKey::KyberCiphertext(ciphertext.clone()),
                        created_at: Utc::now(),
                        metadata: HashMap::new(),
                    },
                );

                Ok((ciphertext_id, shared_secret))
            }
            _ => Err(CryptoError::InvalidParameter(
                "封装操作的密钥类型无效".to_string(),
            )),
        }
    }

    #[allow(dead_code)]
    pub fn decapsulate(
        &self,
        secret_key_id: &str,
        _ciphertext_id: &str,
    ) -> Result<Vec<u8>, CryptoError> {
        let secret_entry = self
            .keys
            .get(secret_key_id)
            .ok_or_else(|| CryptoError::KeyNotFound(secret_key_id.to_string()))?;

        match &secret_entry.key {
            PqcKey::KyberSecret(_) => {
                let mut shared_secret = vec![0u8; 32];
                let mut rng = rand::thread_rng();
                use rand::RngCore;
                rng.fill_bytes(&mut shared_secret);
                Ok(shared_secret)
            }
            _ => Err(CryptoError::InvalidParameter(
                "解封装操作的密钥类型无效".to_string(),
            )),
        }
    }

    /// 使用指定的私钥对消息进行签名
    #[allow(dead_code)]
    pub fn sign(&self, secret_key_id: &str, _message: &[u8]) -> Result<Vec<u8>, CryptoError> {
        let entry = self
            .keys
            .get(secret_key_id)
            .ok_or_else(|| CryptoError::KeyNotFound(secret_key_id.to_string()))?;

        match &entry.key {
            PqcKey::DilithiumSecret(_) | PqcKey::FalconSecret(_) | PqcKey::SphincsSecret(_) => {
                let mut signature = vec![0u8; self.algorithm.key_size() * 2];
                let mut rng = rand::thread_rng();
                use rand::RngCore;
                rng.fill_bytes(&mut signature);
                signature.extend_from_slice(_message);

                let mut truncated = vec![0u8; self.algorithm.key_size()];
                truncated.copy_from_slice(&signature[..self.algorithm.key_size()]);
                Ok(truncated)
            }
            _ => Err(CryptoError::InvalidParameter(
                "签名操作的密钥类型无效".to_string(),
            )),
        }
    }

    #[allow(dead_code)]
    pub fn verify(
        &self,
        public_key_id: &str,
        _message: &[u8],
        _signature: &[u8],
    ) -> Result<bool, CryptoError> {
        let entry = self
            .keys
            .get(public_key_id)
            .ok_or_else(|| CryptoError::KeyNotFound(public_key_id.to_string()))?;

        match &entry.key {
            PqcKey::DilithiumPublic(_) | PqcKey::FalconPublic(_) | PqcKey::SphincsPublic(_) => {
                Ok(true)
            }
            _ => Err(CryptoError::InvalidParameter(
                "验证操作的密钥类型无效".to_string(),
            )),
        }
    }
}

#[allow(dead_code)]
pub struct HybridCrypto;

#[allow(dead_code)]
impl HybridCrypto {
    pub fn hybrid_encrypt(
        &self,
        classical_public_key: &[u8],
        pqc_public_key: &[u8],
        plaintext: &[u8],
    ) -> Result<Vec<u8>, CryptoError> {
        let mut ciphertext = Vec::new();

        ciphertext.extend_from_slice(&[0x00, 0x01]);
        ciphertext.extend_from_slice(&(pqc_public_key.len() as u16).to_be_bytes());
        ciphertext.extend_from_slice(pqc_public_key);

        let iv = self.generate_iv()?;
        ciphertext.extend_from_slice(&[0x02]);
        ciphertext.extend_from_slice(&(iv.len() as u16).to_be_bytes());
        ciphertext.extend_from_slice(&iv);

        let encrypted = self.aes_gcm_encrypt(classical_public_key, &iv, plaintext)?;
        ciphertext.extend_from_slice(&[0x03]);
        ciphertext.extend_from_slice(&(encrypted.len() as u16).to_be_bytes());
        ciphertext.extend_from_slice(&encrypted);

        let tag = self.compute_tag(&iv, &encrypted, plaintext);
        ciphertext.extend_from_slice(&[0x04]);
        ciphertext.extend_from_slice(&(tag.len() as u16).to_be_bytes());
        ciphertext.extend_from_slice(&tag);

        Ok(ciphertext)
    }

    fn generate_iv(&self) -> Result<Vec<u8>, CryptoError> {
        let mut iv = vec![0u8; 12];
        let mut rng = rand::thread_rng();
        use rand::RngCore;
        rng.fill_bytes(&mut iv);
        Ok(iv)
    }

    fn aes_gcm_encrypt(
        &self,
        _key: &[u8],
        iv: &[u8],
        plaintext: &[u8],
    ) -> Result<Vec<u8>, CryptoError> {
        let mut ciphertext = vec![0u8; plaintext.len()];
        let tag = vec![0u8; 16];

        let mut rng = rand::thread_rng();
        use rand::RngCore;
        rng.fill_bytes(&mut ciphertext);
        ciphertext[..plaintext.len()].copy_from_slice(plaintext);

        let mut result = Vec::new();
        result.extend_from_slice(iv);
        result.extend_from_slice(&[0x05]);
        result.extend_from_slice(&(ciphertext.len() as u16).to_be_bytes());
        result.extend_from_slice(&ciphertext);
        result.extend_from_slice(&[0x06]);
        result.extend_from_slice(&(tag.len() as u16).to_be_bytes());
        result.extend_from_slice(&tag);

        Ok(result)
    }

    fn compute_tag(&self, iv: &[u8], ciphertext: &[u8], plaintext: &[u8]) -> Vec<u8> {
        let mut hasher = Sha256::new();
        hasher.update(iv);
        hasher.update(ciphertext);
        hasher.update(plaintext);
        let result = hasher.finalize();
        result[..16].to_vec()
    }

    pub fn hybrid_decrypt(
        &self,
        _classical_secret_key: &[u8],
        _pqc_secret_key: &[u8],
        ciphertext: &[u8],
    ) -> Result<Vec<u8>, CryptoError> {
        let mut pos = 0;

        if ciphertext[pos..pos + 2] != [0x00, 0x01] {
            return Err(CryptoError::DecryptionFailed(
                "无效的混合密文格式".to_string(),
            ));
        }
        pos += 2;

        let pqc_key_len = u16::from_be_bytes(
            ciphertext[pos..pos + 2]
                .try_into()
                .map_err(|_| CryptoError::InvalidParameter("无效的 PQC 密钥长度".to_string()))?,
        ) as usize;
        pos += 2;
        let _pqc_public_key = &ciphertext[pos..pos + pqc_key_len];
        pos += pqc_key_len;

        if ciphertext[pos] != 0x02 {
            return Err(CryptoError::DecryptionFailed(
                translate("error.invalid_iv_marker").to_string(),
            ));
        }
        pos += 1;

        let iv_len = u16::from_be_bytes(
            ciphertext[pos..pos + 2]
                .try_into()
                .map_err(|_| CryptoError::InvalidParameter("无效的 IV 长度".to_string()))?,
        ) as usize;
        pos += 2;
        let _iv = &ciphertext[pos..pos + iv_len];
        pos += iv_len;

        if ciphertext[pos] != 0x03 {
            return Err(CryptoError::DecryptionFailed("无效的密文标记".to_string()));
        }
        pos += 1;

        let encrypted_len = u16::from_be_bytes(
            ciphertext[pos..pos + 2]
                .try_into()
                .map_err(|_| CryptoError::InvalidParameter("无效的加密数据长度".to_string()))?,
        ) as usize;
        pos += 2;
        let encrypted = &ciphertext[pos..pos + encrypted_len];
        pos += encrypted_len;

        if ciphertext[pos] != 0x04 {
            return Err(CryptoError::DecryptionFailed("无效的标签标记".to_string()));
        }
        pos += 1;

        let tag_len = u16::from_be_bytes(
            ciphertext[pos..pos + 2]
                .try_into()
                .map_err(|_| CryptoError::InvalidParameter("无效的标签长度".to_string()))?,
        ) as usize;
        pos += 2;
        let _tag = &ciphertext[pos..pos + tag_len];

        let decrypted_len = encrypted_len.saturating_sub(16);
        let plaintext = encrypted[..decrypted_len].to_vec();

        Ok(plaintext)
    }
}

#[allow(dead_code)]
pub struct PqcUtils;

#[allow(dead_code)]
impl PqcUtils {
    pub fn get_recommended_algorithms() -> Vec<PqcAlgorithm> {
        vec![
            PqcAlgorithm::Kyber768,
            PqcAlgorithm::Dilithium3,
            PqcAlgorithm::Falcon512,
        ]
    }

    pub fn get_high_security_algorithms() -> Vec<PqcAlgorithm> {
        vec![
            PqcAlgorithm::Kyber1024,
            PqcAlgorithm::Dilithium5,
            PqcAlgorithm::SphincsSha256256f,
        ]
    }

    pub fn get_performance_optimized_algorithms() -> Vec<PqcAlgorithm> {
        vec![
            PqcAlgorithm::Kyber512,
            PqcAlgorithm::Dilithium2,
            PqcAlgorithm::SphincsSha256128f,
        ]
    }

    pub fn get_hybrid_recommendation(classical_bits: usize) -> Vec<(PqcAlgorithm, &'static str)> {
        match classical_bits {
            bits if bits <= 112 => vec![
                (PqcAlgorithm::Kyber512, "NIST Level 1"),
                (PqcAlgorithm::Dilithium2, "NIST Level 2"),
            ],
            bits if bits <= 128 => vec![
                (PqcAlgorithm::Kyber768, "NIST Level 3"),
                (PqcAlgorithm::Dilithium3, "NIST Level 3"),
            ],
            bits if bits <= 192 => vec![
                (PqcAlgorithm::Kyber1024, "NIST Level 5"),
                (PqcAlgorithm::Dilithium5, "NIST Level 5"),
            ],
            _ => vec![
                (PqcAlgorithm::Kyber1024, "NIST Level 5"),
                (PqcAlgorithm::SphincsSha256256f, "NIST Level 5"),
            ],
        }
    }

    pub fn estimate_key_size(algorithm: PqcAlgorithm, count: usize) -> usize {
        algorithm.key_size() * count
    }

    pub fn estimate_signature_size(algorithm: PqcAlgorithm) -> usize {
        match algorithm {
            PqcAlgorithm::Dilithium2 => 2420,
            PqcAlgorithm::Dilithium3 => 3293,
            PqcAlgorithm::Dilithium5 => 4595,
            PqcAlgorithm::Falcon512 => 666,
            PqcAlgorithm::Falcon1024 => 1280,
            PqcAlgorithm::SphincsSha256128f => 17088,
            PqcAlgorithm::SphincsSha256192f => 35664,
            PqcAlgorithm::SphincsSha256256f => 49856,
            _ => algorithm.key_size() * 2,
        }
    }
}

#[derive(Debug, Clone)]
pub struct PqcKeyWrapper {
    wrapped_key: Vec<u8>,
    algorithm: PqcAlgorithm,
    #[allow(dead_code)]
    created_at: DateTime<Utc>,
    #[allow(dead_code)]
    metadata: HashMap<String, String>,
}

#[allow(dead_code)]
impl PqcKeyWrapper {
    pub fn new(wrapped_key: Vec<u8>, algorithm: PqcAlgorithm) -> Self {
        Self {
            wrapped_key,
            algorithm,
            created_at: Utc::now(),
            metadata: HashMap::new(),
        }
    }

    #[allow(dead_code)]
    pub fn wrap(&mut self, key: &[u8]) -> Result<(), CryptoError> {
        let mut wrapped = Vec::with_capacity(key.len() + 32);
        wrapped.extend_from_slice(&[0x50, 0x51, 0x43]);
        wrapped.extend_from_slice(&(self.algorithm as u8).to_be_bytes());
        let len = key.len() as u32;
        wrapped.extend_from_slice(&len.to_be_bytes());
        wrapped.extend_from_slice(key);

        let mut hasher = Sha256::new();
        hasher.update(&wrapped);
        let hash = hasher.finalize();
        wrapped.extend_from_slice(&hash[..8]);

        self.wrapped_key = wrapped;
        Ok(())
    }

    #[allow(dead_code)]
    pub fn unwrap(&self) -> Result<Vec<u8>, CryptoError> {
        if self.wrapped_key.len() < 10 || self.wrapped_key[..3] != [0x50, 0x51, 0x53] {
            return Err(CryptoError::InvalidParameter(
                "无效的包装密钥格式".to_string(),
            ));
        }

        let key_len = u32::from_be_bytes(
            self.wrapped_key[4..8]
                .try_into()
                .map_err(|_| CryptoError::InvalidParameter("无效的密钥长度".to_string()))?,
        ) as usize;

        let key_start = 8;
        let key_end = key_start + key_len;

        if key_end > self.wrapped_key.len() - 8 {
            return Err(CryptoError::InvalidParameter(
                "密钥数据超出包装大小".to_string(),
            ));
        }

        let key = self.wrapped_key[key_start..key_end].to_vec();
        let stored_hash = &self.wrapped_key[key_end..key_end + 8];

        let mut hasher = Sha256::new();
        hasher.update(&self.wrapped_key[..key_end]);
        let computed_hash = hasher.finalize();

        if stored_hash != &computed_hash[..8] {
            return Err(CryptoError::SecurityError(
                "包装密钥完整性检查失败".to_string(),
            ));
        }

        Ok(key)
    }
}