Skip to main content

encrypt_document

Function encrypt_document 

Source
pub fn encrypt_document(
    doc: &Document,
    user_password: &str,
    owner_password: &str,
    permissions: Permissions,
    options: WriteOptions,
) -> Result<Vec<u8>>
Expand description

rewrite_document, writing through an encrypting Writer instead of a plain one: every copied string and stream is AES-256 protected under user_password and owner_password (ISO 32000-2 §7.6.4.3), with permissions as the restrictions a reader opening under the user password is granted. An empty owner_password falls back to user_password; both empty is refused, since neither password would then protect the file at all. doc is refused when Document::is_locked, the same refusal Importer::new already raises. An already password-opened encrypted doc is fine: its content already reads as plaintext through Document::get, so it copies across like any unencrypted source and gets encrypted afresh under the new passwords.

Not available on wasm32-unknown-unknown: it builds its Encryptor with Encryptor::aes256, which needs the operating system’s random source. Construct an Encryptor with Encryptor::aes256_with_rng and Writer::new_encrypted directly there instead.