use crate::object::Dictionary;
use crate::permissions::PermissionsConfig;
use crate::security::standard::ObjectKeyAlg;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncryptMethod {
V4Aes128,
V5R6Aes256,
V5R5Aes256,
V1Rc440,
V2Rc4128,
V4Rc4128,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncryptParams {
pub method: EncryptMethod,
pub user_password: Vec<u8>,
pub owner_password: Vec<u8>,
pub permissions: PermissionsConfig,
pub encrypt_metadata: bool,
}
impl EncryptParams {
pub fn v4_aes128(
user_password: impl Into<Vec<u8>>,
owner_password: impl Into<Vec<u8>>,
) -> Self {
Self {
method: EncryptMethod::V4Aes128,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn v5_r6(user_password: impl Into<Vec<u8>>, owner_password: impl Into<Vec<u8>>) -> Self {
Self {
method: EncryptMethod::V5R6Aes256,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn v5_r5(user_password: impl Into<Vec<u8>>, owner_password: impl Into<Vec<u8>>) -> Self {
Self {
method: EncryptMethod::V5R5Aes256,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn rc4(
method: EncryptMethod,
user_password: impl Into<Vec<u8>>,
owner_password: impl Into<Vec<u8>>,
) -> Self {
debug_assert!(
matches!(
method,
EncryptMethod::V1Rc440 | EncryptMethod::V2Rc4128 | EncryptMethod::V4Rc4128
),
"EncryptParams::rc4 requires an RC4 method"
);
Self {
method,
user_password: user_password.into(),
owner_password: owner_password.into(),
permissions: PermissionsConfig::default(),
encrypt_metadata: true,
}
}
pub fn is_weak_rc4(&self) -> bool {
matches!(
self.method,
EncryptMethod::V1Rc440 | EncryptMethod::V2Rc4128 | EncryptMethod::V4Rc4128
)
}
}
#[derive(Debug, Clone)]
pub struct CopyEncryptionSource {
pub encrypt_dict: Dictionary,
pub file_key: Vec<u8>,
pub id0: Vec<u8>,
pub object_key_alg: ObjectKeyAlg,
}