easypdf_core/crypto/encrypt/pdf_encryption.rs
1//! PDF 加密配置。
2
3use super::{PdfEncryptionAlgorithm, PdfPermissions};
4
5/// PDF 加密配置。
6///
7/// 使用构建器方法 [`with_algorithm`](Self::with_algorithm) 和
8/// [`with_permissions`](Self::with_permissions) 进行自定义。默认使用
9/// AES-256 并授予所有权限。
10///
11/// # Examples
12///
13/// ```
14/// use easypdf_core::crypto::{PdfEncryption, PdfEncryptionAlgorithm, PdfPermissions};
15///
16/// let enc = PdfEncryption::new("user", "owner");
17/// assert_eq!(enc.algorithm, PdfEncryptionAlgorithm::Aes256);
18///
19/// let enc = PdfEncryption::new("u", "o")
20/// .with_algorithm(PdfEncryptionAlgorithm::Aes128)
21/// .with_permissions(PdfPermissions::PRINT | PdfPermissions::COPY);
22/// assert_eq!(enc.algorithm, PdfEncryptionAlgorithm::Aes128);
23/// ```
24pub struct PdfEncryption {
25 /// 打开和读取文档所需的密码。
26 pub user_password: String,
27 /// 更改权限或移除加密所需的密码。
28 pub owner_password: String,
29 /// 要使用的加密算法。
30 pub algorithm: PdfEncryptionAlgorithm,
31 /// 用户密码的权限标志。
32 pub permissions: PdfPermissions,
33}
34
35impl PdfEncryption {
36 /// 创建使用 AES-256 和全部权限的加密配置。
37 pub fn new(user: impl Into<String>, owner: impl Into<String>) -> Self {
38 Self {
39 user_password: user.into(),
40 owner_password: owner.into(),
41 algorithm: PdfEncryptionAlgorithm::Aes256,
42 permissions: PdfPermissions::all(),
43 }
44 }
45
46 /// 设置加密算法。
47 #[must_use]
48 pub fn with_algorithm(mut self, algorithm: PdfEncryptionAlgorithm) -> Self {
49 self.algorithm = algorithm;
50 self
51 }
52
53 /// 设置权限标志。
54 #[must_use]
55 pub fn with_permissions(mut self, permissions: PdfPermissions) -> Self {
56 self.permissions = permissions;
57 self
58 }
59}