| 1 |
// Example demonstrating PDF decryption capabilities |
| 2 |
// This verifies the modifications to src/reader.rs for handling encrypted PDFs |
| 3 |
|
| 4 |
use lopdf::{Document, EncryptionState, EncryptionVersion, Permissions}; |
| 5 |
|
| 6 |
#[cfg(not(feature = "async"))] |
| 7 |
fn main() { |
| 8 |
println!("=== PDF Decryption Verification ===\n"); |
| 9 |
|
| 10 |
// Test 1: Load an encrypted PDF from assets |
| 11 |
println!("Test 1: Loading encrypted PDF from assets/encrypted.pdf"); |
| 12 |
match Document::load("assets/encrypted.pdf") { |
| 13 |
Ok(doc) => { |
| 14 |
println!("✓ Successfully loaded encrypted PDF"); |
| 15 |
println!(" - Is encrypted: {}", doc.is_encrypted()); |
| 16 |
println!(" - Number of pages: {}", doc.get_pages().len()); |
| 17 |
println!(" - Has encryption state: {}", doc.encryption_state.is_some()); |
| 18 |
|
| 19 |
// Try to extract text |
| 20 |
let pages = doc.get_pages(); |
| 21 |
let page_nums: Vec<u32> = pages.keys().cloned().collect(); |
| 22 |
match doc.extract_text(&page_nums) { |
| 23 |
Ok(text) => { |
| 24 |
println!(" - Text extraction successful"); |
| 25 |
println!(" - Text length: {} characters", text.len()); |
| 26 |
} |
| 27 |
Err(e) => println!(" - Text extraction failed: {:?}", e), |
| 28 |
} |
| 29 |
} |
| 30 |
Err(e) => println!("✗ Failed to load encrypted PDF: {:?}", e), |
| 31 |
} |
| 32 |
|
| 33 |
println!(); |
| 34 |
|
| 35 |
// Test 2: Create and encrypt a new PDF, then verify it can be loaded |
| 36 |
println!("Test 2: Creating, encrypting, and re-loading a PDF"); |
| 37 |
|
| 38 |
// Create a simple PDF |
| 39 |
let mut doc = Document::with_version("1.5"); |
| 40 |
|
| 41 |
// Add ID (required for encryption) |
| 42 |
doc.trailer.set( |
| 43 |
"ID", |
| 44 |
lopdf::Object::Array(vec![ |
| 45 |
lopdf::Object::String(vec![1u8; 16], lopdf::StringFormat::Literal), |
| 46 |
lopdf::Object::String(vec![2u8; 16], lopdf::StringFormat::Literal), |
| 47 |
]), |
| 48 |
); |
| 49 |
|
| 50 |
// Add minimal structure |
| 51 |
let catalog = doc.add_object(lopdf::dictionary! { |
| 52 |
"Type" => "Catalog", |
| 53 |
"Pages" => lopdf::Object::Reference((2, 0)) |
| 54 |
}); |
| 55 |
doc.trailer.set("Root", lopdf::Object::Reference(catalog)); |
| 56 |
|
| 57 |
doc.objects.insert((2, 0), lopdf::Object::Dictionary(lopdf::dictionary! { |
| 58 |
"Type" => "Pages", |
| 59 |
"Count" => 0, |
| 60 |
"Kids" => Vec::<lopdf::Object>::new() |
| 61 |
})); |
| 62 |
|
| 63 |
// Encrypt the document |
| 64 |
let encryption_version = EncryptionVersion::V2 { |
| 65 |
document: &doc, |
| 66 |
owner_password: "", |
| 67 |
user_password: "", |
| 68 |
key_length: 128, |
| 69 |
permissions: Permissions::all(), |
| 70 |
}; |
| 71 |
|
| 72 |
match EncryptionState::try_from(encryption_version) { |
| 73 |
Ok(state) => { |
| 74 |
doc.encrypt(&state).unwrap(); |
| 75 |
println!("✓ Document encrypted successfully"); |
| 76 |
|
| 77 |
// Save to temporary location |
| 78 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 79 |
let path = temp_dir.path().join("test_encrypted.pdf"); |
| 80 |
doc.save(&path).unwrap(); |
| 81 |
println!("✓ Encrypted document saved"); |
| 82 |
|
| 83 |
// Try to load it back |
| 84 |
match Document::load(&path) { |
| 85 |
Ok(loaded_doc) => { |
| 86 |
println!("✓ Encrypted document re-loaded successfully"); |
| 87 |
println!(" - Is encrypted: {}", loaded_doc.is_encrypted()); |
| 88 |
println!(" - Has encryption state: {}", loaded_doc.encryption_state.is_some()); |
| 89 |
} |
| 90 |
Err(e) => println!("✗ Failed to re-load encrypted document: {:?}", e), |
| 91 |
} |
| 92 |
} |
| 93 |
Err(e) => println!("✗ Failed to create encryption state: {:?}", e), |
| 94 |
} |
| 95 |
|
| 96 |
println!(); |
| 97 |
|
| 98 |
// Test 3: Verify object access in encrypted PDF |
| 99 |
println!("Test 3: Verifying object access in encrypted PDF"); |
| 100 |
if let Ok(doc) = Document::load("assets/encrypted.pdf") { |
| 101 |
let mut accessible_objects = 0; |
| 102 |
let mut total_checked = 0; |
| 103 |
|
| 104 |
for i in 1..=20 { |
| 105 |
total_checked += 1; |
| 106 |
if doc.get_object((i, 0)).is_ok() { |
| 107 |
accessible_objects += 1; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
println!("✓ Object access test completed"); |
| 112 |
println!(" - Objects checked: {}", total_checked); |
| 113 |
println!(" - Objects accessible: {}", accessible_objects); |
| 114 |
println!(" - Success rate: {:.1}%", (accessible_objects as f64 / total_checked as f64) * 100.0); |
| 115 |
} |
| 116 |
|
| 117 |
println!("\n=== All decryption tests completed ==="); |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(feature = "async")] |
| 121 |
#[tokio::main] |
| 122 |
async fn main() { |
| 123 |
println!("=== PDF Decryption Verification (Async) ===\n"); |
| 124 |
|
| 125 |
// Test 1: Load an encrypted PDF from assets |
| 126 |
println!("Test 1: Loading encrypted PDF from assets/encrypted.pdf"); |
| 127 |
match Document::load("assets/encrypted.pdf").await { |
| 128 |
Ok(doc) => { |
| 129 |
println!("✓ Successfully loaded encrypted PDF"); |
| 130 |
println!(" - Is encrypted: {}", doc.is_encrypted()); |
| 131 |
println!(" - Number of pages: {}", doc.get_pages().len()); |
| 132 |
println!(" - Has encryption state: {}", doc.encryption_state.is_some()); |
| 133 |
|
| 134 |
// Try to extract text |
| 135 |
let pages = doc.get_pages(); |
| 136 |
let page_nums: Vec<u32> = pages.keys().cloned().collect(); |
| 137 |
match doc.extract_text(&page_nums) { |
| 138 |
Ok(text) => { |
| 139 |
println!(" - Text extraction successful"); |
| 140 |
println!(" - Text length: {} characters", text.len()); |
| 141 |
} |
| 142 |
Err(e) => println!(" - Text extraction failed: {:?}", e), |
| 143 |
} |
| 144 |
} |
| 145 |
Err(e) => println!("✗ Failed to load encrypted PDF: {:?}", e), |
| 146 |
} |
| 147 |
|
| 148 |
println!(); |
| 149 |
|
| 150 |
// Test 2: Create and encrypt a new PDF, then verify it can be loaded |
| 151 |
println!("Test 2: Creating, encrypting, and re-loading a PDF"); |
| 152 |
|
| 153 |
// Create a simple PDF |
| 154 |
let mut doc = Document::with_version("1.5"); |
| 155 |
|
| 156 |
// Add ID (required for encryption) |
| 157 |
doc.trailer.set( |
| 158 |
"ID", |
| 159 |
lopdf::Object::Array(vec![ |
| 160 |
lopdf::Object::String(vec![1u8; 16], lopdf::StringFormat::Literal), |
| 161 |
lopdf::Object::String(vec![2u8; 16], lopdf::StringFormat::Literal), |
| 162 |
]), |
| 163 |
); |
| 164 |
|
| 165 |
// Add minimal structure |
| 166 |
let catalog = doc.add_object(lopdf::dictionary! { |
| 167 |
"Type" => "Catalog", |
| 168 |
"Pages" => lopdf::Object::Reference((2, 0)) |
| 169 |
}); |
| 170 |
doc.trailer.set("Root", lopdf::Object::Reference(catalog)); |
| 171 |
|
| 172 |
doc.objects.insert((2, 0), lopdf::Object::Dictionary(lopdf::dictionary! { |
| 173 |
"Type" => "Pages", |
| 174 |
"Count" => 0, |
| 175 |
"Kids" => Vec::<lopdf::Object>::new() |
| 176 |
})); |
| 177 |
|
| 178 |
// Encrypt the document |
| 179 |
let encryption_version = EncryptionVersion::V2 { |
| 180 |
document: &doc, |
| 181 |
owner_password: "", |
| 182 |
user_password: "", |
| 183 |
key_length: 128, |
| 184 |
permissions: Permissions::all(), |
| 185 |
}; |
| 186 |
|
| 187 |
match EncryptionState::try_from(encryption_version) { |
| 188 |
Ok(state) => { |
| 189 |
doc.encrypt(&state).unwrap(); |
| 190 |
println!("✓ Document encrypted successfully"); |
| 191 |
|
| 192 |
// Save to temporary location |
| 193 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 194 |
let path = temp_dir.path().join("test_encrypted.pdf"); |
| 195 |
doc.save(&path).unwrap(); |
| 196 |
println!("✓ Encrypted document saved"); |
| 197 |
|
| 198 |
// Try to load it back |
| 199 |
match Document::load(&path).await { |
| 200 |
Ok(loaded_doc) => { |
| 201 |
println!("✓ Encrypted document re-loaded successfully"); |
| 202 |
println!(" - Is encrypted: {}", loaded_doc.is_encrypted()); |
| 203 |
println!(" - Has encryption state: {}", loaded_doc.encryption_state.is_some()); |
| 204 |
} |
| 205 |
Err(e) => println!("✗ Failed to re-load encrypted document: {:?}", e), |
| 206 |
} |
| 207 |
} |
| 208 |
Err(e) => println!("✗ Failed to create encryption state: {:?}", e), |
| 209 |
} |
| 210 |
|
| 211 |
println!(); |
| 212 |
|
| 213 |
// Test 3: Verify object access in encrypted PDF |
| 214 |
println!("Test 3: Verifying object access in encrypted PDF"); |
| 215 |
if let Ok(doc) = Document::load("assets/encrypted.pdf").await { |
| 216 |
let mut accessible_objects = 0; |
| 217 |
let mut total_checked = 0; |
| 218 |
|
| 219 |
for i in 1..=20 { |
| 220 |
total_checked += 1; |
| 221 |
if doc.get_object((i, 0)).is_ok() { |
| 222 |
accessible_objects += 1; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
println!("✓ Object access test completed"); |
| 227 |
println!(" - Objects checked: {}", total_checked); |
| 228 |
println!(" - Objects accessible: {}", accessible_objects); |
| 229 |
println!(" - Success rate: {:.1}%", (accessible_objects as f64 / total_checked as f64) * 100.0); |
| 230 |
} |
| 231 |
|
| 232 |
println!("\n=== All decryption tests completed ==="); |
| 233 |
} |