#[cfg(feature = "crypto")]
use crate::error::{Error, Result};
#[cfg(feature = "crypto")]
pub fn decrypt<P: AsRef<std::path::Path>>(
path: P,
passwords: &[&str],
) -> Result<Vec<u8>> {
let data = std::fs::read(path)?;
decrypt_from_bytes(&data, passwords)
}
#[cfg(feature = "crypto")]
pub fn decrypt_from_bytes(data: &[u8], passwords: &[&str]) -> Result<Vec<u8>> {
for &password in passwords {
match office_crypto::decrypt_from_bytes(data, password) {
Ok(decrypted) => return Ok(decrypted),
Err(_) => continue,
}
}
Err(Error::WrongPassword)
}
#[cfg(feature = "crypto")]
pub fn is_encrypted(data: &[u8]) -> bool {
use crate::ole::container::OleFile;
if let Ok(ole) = OleFile::from_bytes(data) {
let streams = ole.list_streams();
streams.iter().any(|s| {
let lower = s.to_lowercase();
lower.contains("encryptedpackage") || lower.contains("encryptioninfo")
})
} else {
false
}
}