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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# Ciphern 用户指南

欢迎使用 Ciphern!本指南将帮助您从入门到精通,涵盖常见使用场景和最佳实践。

**版本**: v0.1.0  
**更新日期**: 2025-12-22

---

## 目录

1. [安装与配置]#1-安装与配置
2. [核心概念]#2-核心概念
3. [基础使用]#3-基础使用
4. [密钥管理]#4-密钥管理
5. [高级特性]#5-高级特性
6. [多语言集成]#6-多语言集成
7. [生产环境部署]#7-生产环境部署
8. [故障排查]#8-故障排查
9. [最佳实践]#9-最佳实践
10. [常见问题]#10-常见问题

---

## 1. 安装与配置

### 1.1 快速开始

#### 添加依赖

```toml
# Cargo.toml
[dependencies]
ciphern = "0.1"

# 可选特性
[dependencies]
ciphern = { version = "0.1", features = ["fips"] }
```

#### 可用特性 (Features)

| 特性        | 描述              | 建议场景  |
|-----------|-----------------|-------|
| `default` | 包含 std, fips, hash, encrypt, kdf | 默认全功能版本 |
| `std`     | 启用标准库支持         | 基础依赖  |
| `fips`    | FIPS 140-3 合规支持 | 金融、政府 |
| `hash`    | 启用哈希算法 (SHA/SM3) | 数据完整性校验 |
| `encrypt` | 启用加解密与签名接口   | 核心加密功能 |
| `kdf`     | 启用密钥派生功能      | 密钥管理高级场景 |
| `plugin`  | 启用动态插件加载      | 自定义算法扩展 |

### 1.2 初始化

在使用任何功能前,建议调用 `init()` 函数以确保系统自检(如 FIPS 自检)和审计日志初始化。

```rust
use ciphern::Result;

fn main() -> Result<()> {
    // 初始化系统
    ciphern::init()?;
    Ok(())
}
```

---

## 2. 核心概念

### 2.1 算法 (Algorithm)

Ciphern 支持多种加密算法,分为国际标准和国密标准。

#### 对称加密

| 算法              | 密钥长度    | 性能 | 使用场景   |
|-----------------|---------|----|--------|
| **AES128GCM**   | 128 bit | 极快 | 通用数据加密 |
| **AES192GCM**   | 192 bit | 极快 | 通用数据加密 |
| **AES256GCM**   | 256 bit | 极快 | 通用数据加密 |
| **SM4GCM**      | 128 bit || 国密合规场景 |

#### 非对称加密与签名

| 算法             | 安全级别    | 性能 | 使用场景   |
|----------------|---------|----|--------|
| **ECDSAP256**  | 128 bit || 数字签名   |
| **ECDSAP384**  | 192 bit || 数字签名   |
| **ECDSAP521**  | 256 bit || 数字签名   |
| **RSA2048**    | 112 bit || 兼容性场景  |
| **RSA3072**    | 128 bit || 兼容性场景  |
| **RSA4096**    | 152 bit || 兼容性场景  |
| **SM2**        | 128 bit || 国密数字签名 |
| **Ed25519**    | 128 bit | 极快 | 高性能签名  |

#### 哈希函数

| 算法           | 输出长度    | 性能 | 使用场景  |
|--------------|---------|----|-------|
| **SHA256**   | 256 bit || 通用哈希  |
| **SHA384**   | 384 bit || 通用哈希  |
| **SHA512**   | 512 bit || 通用哈希  |
| **SHA3_256** | 256 bit || 高安全哈希 |
| **SM3**      | 256 bit || 国密合规  |

### 2.2 密钥管理器 (KeyManager)

在 Ciphern 中,密钥材料受内存保护,不直接暴露。所有操作通过 `KeyManager` 引用 `key_id` 完成。

---

## 3. 基础使用

### 3.1 加密与解密

```rust
use ciphern::{Cipher, Algorithm, KeyManager, Result};

fn main() -> Result<()> {
    // 1. 初始化系统
    ciphern::init()?;

    // 2. 初始化密钥管理器
    let km = KeyManager::new()?;
    
    // 3. 生成密钥并获得 ID
    let key_id = km.generate_key(Algorithm::AES256GCM)?;
    
    // 4. 创建加密器
    let cipher = Cipher::new(Algorithm::AES256GCM)?;
    
    // 5. 执行加密
    let plaintext = b"Sensitive data";
    let ciphertext = cipher.encrypt(&km, &key_id, plaintext)?;
    
    // 6. 执行解密
    let decrypted = cipher.decrypt(&km, &key_id, &ciphertext)?;
    assert_eq!(plaintext, &decrypted[..]);
    
    Ok(())
}
```

### 3.2 数字签名

```rust
use ciphern::{Signer, Algorithm, KeyManager, Result};

fn main() -> Result<()> {
    // 初始化
    ciphern::init()?;
    
    let km = KeyManager::new()?;
    let key_id = km.generate_key(Algorithm::ECDSAP384)?;
    
    let signer = Signer::new(Algorithm::ECDSAP384)?;
    let message = b"Message to sign";
    
    // 签名
    let signature = signer.sign(&km, &key_id, message)?;
    
    // 验证
    let is_valid = signer.verify(&km, &key_id, message, &signature)?;
    assert!(is_valid);
    
    Ok(())
}
```

### 3.3 消息摘要与哈希

Ciphern 支持多种哈希算法,包括国际标准的 SHA 系列和国密标准的 SM3。注意需要启用 `hash` 特性。

#### 计算 SM3 哈希

```rust
use ciphern::{Hash, Result};

fn main() -> Result<()> {
    let data = b"abc";
    let hash = Hash::sm3(data)?;
    println!("SM3: {:x?}", hash);
    Ok(())
}
```

#### 计算 SHA512 哈希

```rust
use ciphern::{Hash, Result};

fn main() -> Result<()> {
    let data = b"abc";
    let hash = Hash::sha512(data)?;
    println!("SHA-512: {:x?}", hash);
    Ok(())
}
```

### 3.4 密钥别名管理

Ciphern 支持为密钥设置易读的别名,方便管理和检索。

```rust
let km = KeyManager::new()?;

// 生成带别名的密钥
let key_id = km.generate_key_with_alias(Algorithm::AES256GCM, "master-key")?;

// 之后可以通过别名获取密钥 ID
let resolved_id = km.resolve_alias("master-key")?;
assert_eq!(key_id, resolved_id);

// 也可以直接通过别名进行加解密
let cipher = Cipher::new(Algorithm::AES256GCM)?;
let ciphertext = cipher.encrypt(&km, "master-key", b"data")?;
```

---

## 4. 密钥管理

### 4.1 密钥生命周期

```rust
use ciphern::{KeyManager, Algorithm, Result};

fn key_lifecycle_example() -> Result<()> {
    let km = KeyManager::new()?;
    
    // 1. 生成密钥
    let key_id = km.generate_key(Algorithm::AES256GCM)?;
    println!("Generated Key ID: {}", key_id);
    
    // 2. 激活密钥 (新生成的密钥默认已激活)
    km.activate_key(&key_id)?;
    
    // 3. 暂停密钥
    km.suspend_key(&key_id)?;
    
    // 4. 获取密钥信息
    let key = km.get_key(&key_id)?;
    println!("Key State: {:?}", key.state());
    
    Ok(())
}
```

### 4.2 密钥保护机制

Ciphern 提供了多层密钥保护:

- **内存擦除**: 密钥材料在使用后会被 `zeroize` 自动擦除。
- **内存锁定**: 防止密钥被交换到磁盘。
- **访问控制**: 必须通过 `KeyManager` 引用 ID 访问密钥。

### 4.3 密钥状态管理

密钥是加密的核心,Ciphern 提供完整的密钥生命周期管理。

#### 密钥状态

```
PENDING → ACTIVE → ROTATING → DEPRECATED → DESTROYED
```

- **PENDING**: 已生成但未激活
- **ACTIVE**: 正常使用中
- **ROTATING**: 轮换中 (新旧密钥并存)
- **DEPRECATED**: 只能解密,不能加密
- **DESTROYED**: 已销毁,密钥材料已擦除

#### 密钥层次结构

```
Root Key (硬件保护)
  ├─ Tenant A Master Key
  │   ├─ App Key: database-encryption
  │   └─ App Key: file-encryption
  └─ Tenant B Master Key
      └─ App Key: api-encryption
```

### 4.4 密钥派生 (KDF)

从主密钥派生应用密钥,隔离风险。

```rust
use ciphern::Algorithm;
use ciphern::key::manager::KeyManager;

let km = KeyManager::new()?;
let key_id = km.generate_key(Algorithm::HKDF)?;
// 实际派生操作通常通过特定 Provider 完成
```

---

## 5. 高级特性

### 5.1 FIPS 140-3 模式

当启用 `fips` 特性时,Ciphern 可以运行在合规模式:

- 只允许 FIPS 批准的算法(如 AES, SHA-256)。
- 启动时执行自检。
- 强制执行连续随机数生成器测试。

```rust
use ciphern::{fips, Result};

fn main() -> Result<()> {
    // 检查 FIPS 模式是否启用
    if fips::is_fips_enabled() {
        println!("Running in FIPS 140-3 mode");
    }
    Ok(())
}
```

### 5.2 侧信道防护

Ciphern 实现了针对侧信道攻击的防护,包括恒定时间操作以防止定时攻击。

```rust
use ciphern::side_channel::constant_time_eq;

// 示例:恒定时间比较
let a = b"password";
let b = b"password";
let is_equal = constant_time_eq(a, b);
```

### 5.3 密钥状态管理

Ciphern 提供完整的密钥生命周期管理。

```rust
use ciphern::key::KeyState;

fn check_key_state(manager: &KeyManager, key_id: &str) -> Result<()> {
    let key = manager.get_key(key_id)?;
    
    // 检查密钥是否处于 ACTIVE 状态
    assert_eq!(key.state(), KeyState::Active);
    
    Ok(())
}
```

#### 密钥销毁

```rust
// 密钥在 KeyManager 内部由 zeroize 自动擦除
// 显式删除密钥
// km.delete_key(key_id)?;
```

### 4.5 多租户密钥隔离

Ciphern 通过 `KeyManager` 支持基于 ID 的密钥生成,便于在多租户环境中隔离密钥。

```rust
fn multi_tenant_example(km: &KeyManager) -> Result<()> {
    // 为租户 A 生成密钥
    let key_id_a = km.generate_key_with_id(Algorithm::AES256GCM, "tenant-a-key-1")?;

    // 为租户 B 生成密钥
    let key_id_b = km.generate_key_with_id(Algorithm::AES256GCM, "tenant-b-key-1")?;

    Ok(())
}
```

#### 租户访问控制

在应用层,您可以根据当前上下文租户 ID 来拼接密钥 ID,从而实现逻辑隔离。

```rust
let tenant_id = "tenant-123";
let key_id = format!("{}-db-key", tenant_id);
let ciphertext = cipher.encrypt(&km, &key_id, plaintext)?;
```

### 4.6 密钥派生与分层

#### HKDF 密钥派生

```rust
use ciphern::KeyManager;
use ciphern::Algorithm;

fn derive_keys_example(km: &KeyManager) -> Result<()> {
    // 主密钥 ID
    let master_key_id = km.generate_key(Algorithm::AES256GCM)?;
    
    // 派生算法示例
    let kdf_key_id = km.generate_key(Algorithm::HKDF)?;
    
    Ok(())
}
```

---

## 5. 高级特性

### 5.1 FIPS 140-3 合规模式

#### 启用 FIPS 模式

在 `Cargo.toml` 中启用 `fips` 特性,并确保在代码中调用了 `ciphern::init()`。

```toml
# Cargo.toml
[dependencies]
ciphern = { version = "0.1", features = ["fips"] }
```

```rust
use ciphern::{fips, Result};

fn main() -> Result<()> {
    // 初始化(将触发 FIPS 自检)
    ciphern::init()?;

    // 检查 FIPS 模式是否启用
    if ciphern::is_fips_enabled() {
        println!("✅ FIPS mode enabled");
    }

    Ok(())
}
```

#### FIPS 批准的算法

| 类别   | 批准算法                                   | 不批准算法        |
|------|----------------------------------------|--------------|
| 对称加密 | AES128GCM, AES192GCM, AES256GCM        | SM4GCM       |
| 非对称  | ECDSAP256, ECDSAP384, ECDSAP521, RSA2048, RSA3072, RSA4096 | SM2, Ed25519 |
| 哈希   | SHA256, SHA384, SHA512, SHA3_256       | SM3          |
| KDF  | HKDF, PBKDF2                           | Sm3Kdf       |

### 5.2 性能优化

#### 自动检测与使用

Ciphern 会自动检测 CPU 特性并使用最优实现(如 AES-NI, AVX2, ARM Crypto Extensions)。

#### 性能监控 (Prometheus)

Ciphern 内部集成了 Prometheus 指标。

```rust
use ciphern::audit::REGISTRY;

// 获取指标
let metrics = REGISTRY.gather();
```

### 5.3 审计日志

#### 启用审计

在 `ciphern::init()` 调用后,审计日志会自动按需记录。

```rust
use ciphern::audit::AuditLogger;

// 记录自定义操作
AuditLogger::log("CUSTOM_OP", Some(Algorithm::AES256GCM), Some("key-1"), Ok(()));
```

### 5.4 侧信道防护

Ciphern 实现了针对侧信道攻击的多层防护机制,核心是通过恒定时间操作防止定时攻击和功耗分析。

#### 恒定时间操作实现

所有敏感比较和数据处理都使用恒定时间算法,避免因分支预测或内存访问模式泄露信息:

```rust
use ciphern::side_channel::constant_time::constant_time_eq;
use ciphern::side_channel::constant_time::constant_time_less_than;

// 示例1:恒定时间比较(防止时序攻击)
let a = b"password";
let b = b"password";
let is_equal = constant_time_eq(a, b);

// 示例2:恒定时间小于比较
let x = 0x12345678u32;
let y = 0x87654321u32;
let is_less = constant_time_less_than(x, y);
```

#### 加密操作的侧信道防护

Ciphern 内部的所有加密操作(如 AES 加解密、ECDSA 签名)都使用恒定时间实现:

```rust
use ciphern::{Cipher, Algorithm, KeyManager};

// 加密操作自动使用恒定时间实现
let km = KeyManager::new()?;
let key_id = km.generate_key(Algorithm::AES256GCM)?;
let cipher = Cipher::new(Algorithm::AES256GCM)?;

// 该加密操作在恒定时间内完成,不会泄露明文或密钥信息
let ciphertext = cipher.encrypt(&km, &key_id, b"sensitive data")?;
```

#### 内存访问模式防护

Ciphern 使用 `SecretBytes` 容器保护敏感数据,确保内存访问模式不会泄露信息:

```rust
use ciphern::memory::SecretBytes;

// 敏感数据存储在受保护的容器中
let secret = SecretBytes::new(b"secret key material".to_vec())?;

// 所有访问都通过恒定时间接口
let data = secret.as_slice();
```

#### 防护范围

- **定时攻击防护**: 所有比较操作使用恒定时间实现
-**功耗分析防护**: 内存访问模式恒定
-**缓存攻击防护**: 避免数据依赖的内存访问
-**分支预测防护**: 消除基于秘密数据的分支

### 5.5 插件系统

Ciphern 提供了强大的插件系统,允许您扩展库的功能。自定义插件可以提供新的加密算法、实现特定领域的加密逻辑,或集成第三方加密模块。

#### 启用插件功能

在 `Cargo.toml` 中启用 `plugin` 特性:

```toml
[dependencies]
ciphern = { version = "0.1", features = ["plugin"] }
```

#### 核心 API

插件系统提供以下主要组件:

| 类型 | 描述 |
|------|------|
| `PluginManager` | 插件管理器,负责注册、查询和监控插件 |
| `Plugin` trait | 所有插件必须实现的核心 trait |
| `CipherPlugin` trait | 提供加密功能的插件 trait |
| `PluginMetadata` | 插件元数据描述 |

#### 创建自定义插件

##### 实现 Plugin trait

所有插件必须实现 `Plugin` trait,这是插件系统的核心接口:

```rust
use ciphern::plugin::{Plugin, CipherPlugin};
use ciphern::provider::SymmetricCipher;
use ciphern::types::Algorithm;
use ciphern::error::Result;
use std::sync::Arc;
use std::any::Any;

struct MyCustomPlugin {
    name: String,
    version: String,
    initialized: bool,
}

impl MyCustomPlugin {
    pub fn new(name: &str, version: &str) -> Self {
        Self {
            name: name.to_string(),
            version: version.to_string(),
            initialized: false,
        }
    }
}

impl Plugin for MyCustomPlugin {
    fn name(&self) -> &str {
        &self.name
    }

    fn version(&self) -> &str {
        &self.version
    }

    fn initialize(&mut self) -> Result<()> {
        self.initialized = true;
        Ok(())
    }

    fn shutdown(&mut self) -> Result<()> {
        self.initialized = false;
        Ok(())
    }

    fn health_check(&self) -> Result<bool> {
        Ok(self.initialized)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}
```

##### 实现 CipherPlugin trait

如果您的插件需要提供加密功能,还需要实现 `CipherPlugin` trait:

```rust
use ciphern::cipher::provider::AesGcmProvider;

impl CipherPlugin for MyCustomPlugin {
    fn as_symmetric_cipher(&self) -> Arc<dyn SymmetricCipher> {
        Arc::new(AesGcmProvider::new())
    }

    fn supported_algorithms(&self) -> Vec<Algorithm> {
        vec![Algorithm::AES256GCM, Algorithm::AES192GCM, Algorithm::AES128GCM]
    }
}
```

#### 使用 PluginManager

`PluginManager` 提供了完整的插件生命周期管理功能:

```rust
use ciphern::plugin::{PluginManager, Plugin, CipherPlugin};
use std::sync::Arc;

// 创建插件管理器
let manager = PluginManager::new();

// 创建并注册插件
let my_plugin = Arc::new(MyCustomPlugin::new("my-custom-plugin", "1.0.0"));
manager.register_plugin(my_plugin.clone())?;

// 注册加密插件
manager.register_cipher_plugin(my_plugin as Arc<dyn CipherPlugin>)?;

// 列出所有已注册的插件
let plugins = manager.list_plugins();
println!("已注册的插件: {:?}", plugins);

// 获取指定插件
if let Some(plugin) = manager.get_plugin("my-custom-plugin") {
    println!("插件名称: {}", plugin.name());
    println!("插件版本: {}", plugin.version());
}

// 健康检查
let health_status = manager.health_check_all();
for (name, is_healthy) in health_status {
    println!("插件 {} 健康状态: {}", name, if is_healthy { "正常" } else { "异常" });
}
```

#### 完整的插件示例

以下是一个完整的自定义加密插件实现示例:

```rust
use ciphern::plugin::{Plugin, CipherPlugin, PluginManager};
use ciphern::cipher::provider::SymmetricCipher;
use ciphern::types::Algorithm;
use ciphern::error::Result;
use std::sync::Arc;
use std::any::Any;

struct CustomAesPlugin {
    name: String,
    version: String,
}

impl CustomAesPlugin {
    pub fn new() -> Self {
        Self {
            name: "custom-aes-plugin".to_string(),
            version: "1.0.0".to_string(),
        }
    }
}

impl Plugin for CustomAesPlugin {
    fn name(&self) -> &str { &self.name }
    fn version(&self) -> &str { &self.version }
    fn initialize(&mut self) -> Result<()> { Ok(()) }
    fn shutdown(&mut self) -> Result<()> { Ok(()) }
    fn health_check(&self) -> Result<bool> { Ok(true) }
    fn as_any(&self) -> &dyn Any { self }
}

impl CipherPlugin for CustomAesPlugin {
    fn as_symmetric_cipher(&self) -> Arc<dyn SymmetricCipher> {
        // 返回自定义的 SymmetricCipher 实现
        todo!("实现您的自定义加密器")
    }

    fn supported_algorithms(&self) -> Vec<Algorithm> {
        vec![Algorithm::AES256GCM]
    }
}

fn main() -> Result<()> {
    // 初始化 Ciphern
    ciphern::init()?;

    // 创建插件管理器
    let manager = PluginManager::new();

    // 创建并注册插件
    let custom_plugin = Arc::new(CustomAesPlugin::new());
    manager.register_cipher_plugin(custom_plugin)?;

    // 验证插件已注册
    let plugins = manager.list_plugins();
    assert!(plugins.contains(&"custom-aes-plugin".to_string()));

    println!("✅ 插件注册成功!");

    Ok(())
}
```

#### 插件元数据

`PluginMetadata` 结构用于描述插件的元信息,便于插件管理和发现:

```rust
use ciphern::plugin::PluginMetadata;

let metadata = PluginMetadata {
    name: "my-plugin".to_string(),
    version: "1.0.0".to_string(),
    author: "Plugin Developer".to_string(),
    description: "Custom cipher plugin for specialized encryption".to_string(),
    dependencies: vec!["ciphern".to_string()],
    checksum: "sha256:...".to_string(),
};
```

#### 插件最佳实践

1. **线程安全**: 确保您的插件实现是线程安全的,使用 `Send + Sync` bound
2. **错误处理**: 在 `initialize()``shutdown()` 中妥善处理错误
3. **资源管理**: 在 `shutdown()` 中释放所有占用的资源
4. **健康检查**: 实现可靠的健康检查逻辑,便于监控系统检测插件状态
5. **版本管理**: 清晰标注插件版本,避免版本冲突

#### 注意事项

- 插件系统目前处于基础框架阶段,动态加载功能正在完善中
- 建议在生产环境中使用经过充分测试的稳定插件
- 插件错误可能会影响整体加密操作,请确保插件质量

---

## 6. 多语言集成

### 6.1 Java 集成

#### 编译说明

Java JNI 绑定已完成核心功能实现,支持加密解密和密钥管理。目前需要从源码编译:

```bash
# 编译 JNI 库
cargo build --release --features java
```

#### 基础使用

```java
import com.ciphern.Ciphern;

public class Example {
    public static void main(String[] args) {
        // 初始化
        Ciphern.init();
        
        // 生成密钥
        String keyId = Ciphern.generateKey("AES256GCM");
        
        // 加密
        byte[] plaintext = "Hello, Java!".getBytes();
        byte[] ciphertext = Ciphern.encrypt(keyId, plaintext);
        
        // 解密
        byte[] decrypted = Ciphern.decrypt(keyId, ciphertext);
        
        System.out.println("Decrypted: " + new String(decrypted));
    }
}
```

### 6.2 Python 集成

#### 编译说明

Python PyO3 绑定已完成核心功能实现,支持加密解密、签名验证和哈希计算。目前需要从源码编译:

```bash
# 编译 PyO3 扩展
cargo build --release --features python
```

#### 基础使用

```python
from ciphern_py import KeyManager, Ciphern

# 初始化密钥管理器
km = KeyManager()

# 生成密钥
key_id = km.generate_key("AES256GCM")

# 创建加密器
cipher = Ciphern(km)

# 加密
plaintext = b"Hello, Python!"
ciphertext = cipher.encrypt(key_id, plaintext)

# 解密
decrypted = cipher.decrypt(key_id, ciphertext)
assert plaintext == decrypted
```

---

## 7. 生产环境部署

### 7.1 性能调优

#### 线程池配置

Ciphern 使用 Tokio 作为异步运行时,可以通过环境变量或运行时句柄配置线程池。

```bash
# 设置 Tokio 工作线程数
export TOKIO_WORKER_THREADS=8
```

#### SIMD 优化

```bash
# 编译时启用 CPU 特性以获得最佳性能
RUSTFLAGS="-C target-cpu=native" cargo build --release
```

### 7.2 安全加固

#### 内存保护

Ciphern 默认启用多层内存保护机制:

1. **自动内存擦除**: 使用 `zeroize` 自动清理密钥内存
2. **内存锁定**: 使用 `mlock` 防止密钥被交换到磁盘
3. **完整性校验**: 密钥完整性检查防止内存篡改

在 Linux 环境下,确保应用有足够的权限使用 `mlock`:

```bash
# 提升内存锁定限制 (Linux)
# 编辑 /etc/security/limits.conf
* soft memlock unlimited
* hard memlock unlimited
```

#### 敏感数据容器

Ciphern 提供 `SecretBytes` 容器用于存储敏感数据:

```rust
use ciphern::memory::SecretBytes;

// 创建安全容器
let secret = SecretBytes::new(b"sensitive data".to_vec())?;

// 访问数据(自动零化)
let data = secret.as_slice();

// 容器销毁时自动零化内存
```

#### 文件权限

确保密钥存储目录权限正确:

```bash
# 密钥存储目录权限
chmod 700 /path/to/keys
```

### 7.3 监控与告警

#### Prometheus 关键指标

- `crypto_operations_total`: 操作总数 (加密/解密/签名等)
- `crypto_operation_duration_seconds`: 操作延迟
- `crypto_errors_total`: 错误总数

---

## 8. 故障排查

### 8.1 常见错误

#### 错误: `DecryptionFailed`

**原因**:
- 使用了错误的密钥 ID。
- 密文被篡改。
- 算法参数不匹配。

#### 错误: `FipsSelfTestFailed`

**原因**:
- 启用 FIPS 模式时,启动自检失败。
- 环境不满足 FIPS 要求。

### 8.2 调试技巧

#### 启用详细日志

```bash
# 设置日志级别
export RUST_LOG=ciphern=debug
./your_application
```

---

## 9. 最佳实践

### 9.1 安全建议

1. **始终调用 `init()`**: 确保系统正确初始化。
2. **使用别名**: 为关键密钥设置别名,避免在代码中硬编码密钥 ID。
3. **启用审计**: 在生产环境中启用审计日志以满足合规性要求。
4. **定期更新**: 及时更新 Ciphern 以获取最新的安全补丁。

---

## 10. 常见问题 (FAQ)

**Q: 如何选择加密算法?**
A: 推荐使用 `AES256GCM` 处理对称加密,`ECDSAP384` 处理数字签名。

**Q: 是否支持 SM 系列算法?**
A: 是的,Ciphern 支持 SM2, SM3, SM4 算法(注意:非 FIPS 批准)。

**Q: 密钥如何持久化?**
A: 默认情况下,`KeyManager` 会将密钥持久化到配置的存储路径中。

---

## 附录

### A. 完整 API 参考

请参考生成的 Rust 文档:
```bash
cargo doc --open
```

### B. 文档版本

**版本**: v0.1.0
**更新日期**: 2025-12-24
**反馈**: [GitHub Issues](https://github.com/Kirky-X/ciphern/issues)

[⬆ 回到顶部](#ciphern-用户指南)