A unified Rust crate that combines PDF text extraction and digital signature verification. Provides a simple interface for verifying that specific text appears in signed PDF documents.
The core crate combines the functionality of extractor and signature-validator to provide a unified interface for PDF verification. It verifies both the digital signature and the presence of specific text at exact positions within the document.
use core::{verify_text, PdfVerificationResult};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Read PDF file
let pdf_bytes = std::fs::read("signed_document.pdf")?;
// Verify text at specific position
let result = verify_text(pdf_bytes, 0, "Important Document", 100)?;
if result.substring_matches {
println!("✅ Text found at position 100 on page 1");
println!("Signature valid: {}", result.signature.is_valid);
println!("Signer: {}", result.signature.signer_info);
} else {
println!("❌ Text not found at specified position");
}
Ok(())
}pub fn verify_text(
pdf_bytes: Vec<u8>,
page_number: u8,
sub_string: &str,
offset: usize,
) -> Result<PdfVerificationResult, String>Parameters:
pdf_bytes: Raw PDF file bytespage_number: Page number (0-indexed) to searchsub_string: Text to search foroffset: Byte offset within the page text
Returns:
Ok(PdfVerificationResult): Combined verification resultErr(String): Error if verification fails
pub struct PdfVerificationResult {
pub substring_matches: bool, // Text found at exact position
pub signature: PdfSignatureResult, // Signature verification details
}
pub struct PdfSignatureResult {
pub is_valid: bool, // Signature validity
pub message_digest: Vec<u8>, // Extracted message digest
pub public_key: Vec<u8>, // Signer's public key
pub signer_info: String, // Signer information
pub signing_time: Option<String>, // Signing timestamp
pub signature_algorithm: String, // Used algorithm
}The verify_text function performs a comprehensive verification:
- Verifies PDF digital signature authenticity
- Checks content integrity using ByteRange
- Validates cryptographic signatures
- Extracts text from all PDF pages
- Handles various font encodings
- Processes compressed streams
- Checks if
sub_stringappears at exactoffset - Validates text positioning within specified page
- Returns boolean result for proof systems
use core::verify_text;
let pdf_bytes = std::fs::read("document.pdf")?;
let result = verify_text(pdf_bytes, 0, "Sample Text", 50)?;
if result.substring_matches {
println!("Text found at position 50 on page 1");
println!("Signature valid: {}", result.signature.is_valid);
}use core::verify_text;
fn verify_document_structure(pdf_bytes: Vec<u8>) -> Result<(), String> {
// Check title on first page
let title_result = verify_text(pdf_bytes.clone(), 0, "Document Title", 0)?;
if !title_result.substring_matches {
return Err("Title not found on first page".to_string());
}
// Check signature on last page
let signature_result = verify_text(pdf_bytes, 2, "Digitally signed", 200)?;
if !signature_result.substring_matches {
return Err("Signature not found on last page".to_string());
}
println!("Document structure verified successfully");
Ok(())
}Run the basic test suite:
cargo test -p coreRun tests with sample PDFs:
cargo test -p core --features private_tests- ✅ Combined signature and text verification
- ✅ Exact position matching
- ✅ Multi-page document support
- ✅ Standard PDF structures
- ✅ Common font encodings
- ❌ Regex-based text search
- ❌ Image or graphics verification
- ❌ Form field verification
When contributing to the core crate:
- Keep dependencies minimal
- Ensure ZK-VM compatibility
- Add tests for new verification patterns
- Document any breaking changes
- Maintain performance for simple PDFs
This crate is licensed under the same terms as the parent repository.