Skip to main content

encrypt_file

Function encrypt_file 

Source
pub fn encrypt_file(
    input_path: impl AsRef<Path>,
    output_path: impl AsRef<Path>,
    key: &[u8],
    algorithm: Algorithm,
) -> Result<()>
Available on crate features stream and std only.
Expand description

Encrypt the file at input_path into output_path using key and the given AEAD algorithm. Uses the default chunk size (64 KiB).

The output file is overwritten if it already exists. On any failure after the output file has been opened, callers should treat the output file as junk and remove it.

§Errors

  • Error::InvalidKey if key is not 32 bytes.
  • Error::RandomFailure if the OS RNG cannot produce a nonce.
  • Error::Mac for I/O failures (file open, read, write) — the variant carries a &'static str reason; the underlying std::io::Error is not surfaced (would risk leaking path fragments through error rendering).
  • Error::AuthenticationFailed for the (unreachable in practice) AEAD failure path.

§Example

use crypt_io::Algorithm;
use crypt_io::stream;

let key = [0u8; 32];
stream::encrypt_file("input.bin", "output.enc", &key, Algorithm::ChaCha20Poly1305)?;