[][src]Crate age

Library for encrypting and decryping age files

age is a simple, secure, and modern encryption tool with small explicit keys, no config options, and UNIX-style composability.

The age specification is available in a Google Doc here: A simple file encryption tool & format.

Caution: all crate versions prior to 1.0 are beta releases for testing purposes only.

Examples

Key-based encryption

use std::io::{Read, Write};

let key = age::SecretKey::generate();
let pubkey = key.to_public();

let plaintext = b"Hello world!";

// Encrypt the plaintext to a ciphertext...
let encrypted = {
    let encryptor = age::Encryptor::with_recipients(vec![pubkey]);

    let mut encrypted = vec![];
    let mut writer = encryptor.wrap_output(&mut encrypted, age::Format::Binary)?;
    writer.write_all(plaintext)?;
    writer.finish()?;

    encrypted
};

// ... and decrypt the obtained ciphertext to the plaintext again.
let decrypted = {
    let decryptor = match age::Decryptor::new(&encrypted[..])? {
        age::Decryptor::Recipients(d) => d,
        _ => unreachable!(),
    };

    let mut decrypted = vec![];
    let mut reader = decryptor.decrypt(&[key.into()])?;
    reader.read_to_end(&mut decrypted);

    decrypted
};

assert_eq!(decrypted, plaintext);

Passphrase-based encryption

use secrecy::Secret;
use std::io::{Read, Write};

let plaintext = b"Hello world!";
let passphrase = "this is not a good passphrase";

// Encrypt the plaintext to a ciphertext using the passphrase...
let encrypted = {
    let encryptor = age::Encryptor::with_user_passphrase(Secret::new(passphrase.to_owned()));

    let mut encrypted = vec![];
    let mut writer = encryptor.wrap_output(&mut encrypted, age::Format::Binary)?;
    writer.write_all(plaintext)?;
    writer.finish()?;

    encrypted
};

// ... and decrypt the ciphertext to the plaintext again using the same passphrase.
let decrypted = {
    let decryptor = match age::Decryptor::new(&encrypted[..])? {
        age::Decryptor::Passphrase(d) => d,
        _ => unreachable!(),
    };

    let mut decrypted = vec![];
    let mut reader = decryptor.decrypt(&Secret::new(passphrase.to_owned()), None)?;
    reader.read_to_end(&mut decrypted);

    decrypted
};

assert_eq!(decrypted, plaintext);

Re-exports

pub use keys::SecretKey;

Modules

decryptor

Decryptors for age.

keys

Key structs and serialization.

stream

I/O helper structs for age file encryption and decryption.

Structs

Encryptor

Encryptor for creating an age file.

Enums

Decryptor

Decryptor for an age file.

Error

The various errors that can be returned during the decryption process.

Format

Format of output

Traits

Callbacks

Callbacks that might be triggered during decryption.