Crate abcrypt

Source
Expand description

The abcrypt crate is an implementation of the abcrypt encrypted data format.

This crate supports the abcrypt version 1 file format.

§Examples

§Encryption and decryption

use abcrypt::{argon2::Params, Decryptor, Encryptor};

let data = b"Hello, world!\n";
let passphrase = "passphrase";

// Encrypt `data` using `passphrase`.
let params = Params::new(32, 3, 4, None).unwrap();
let ciphertext = Encryptor::with_params(data, passphrase, params)
    .map(|c| c.encrypt_to_vec())
    .unwrap();
assert_ne!(ciphertext, data);

// And decrypt it back.
let plaintext = Decryptor::new(&ciphertext, passphrase)
    .and_then(|c| c.decrypt_to_vec())
    .unwrap();
assert_eq!(plaintext, data);

§no_std support

This crate supports no_std mode and can be used without the alloc crate and the std crate. Disables the default feature to enable this.

Note that the memory blocks used by Argon2 when calculating a derived key is limited to 256 KiB when the alloc feature is disabled.

use abcrypt::{
    argon2::{Algorithm, Params, Version},
    Argon2, Decryptor, Encryptor,
};

let data = b"Hello, world!\n";
let passphrase = "passphrase";

// Encrypt `data` using `passphrase`.
let params = Params::new(32, 3, 4, None).unwrap();
let cipher = Encryptor::with_params(data, passphrase, params).unwrap();
let mut buf = [u8::default(); 178];
cipher.encrypt(&mut buf);
assert_ne!(buf.as_slice(), data);

let argon2 = Argon2::new(buf).unwrap();
assert_eq!(argon2.variant(), Algorithm::Argon2id);
assert_eq!(argon2.version(), Version::V0x13);

// And decrypt it back.
let cipher = Decryptor::new(&buf, passphrase).unwrap();
let mut buf = [u8::default(); 14];
cipher.decrypt(&mut buf).unwrap();
assert_eq!(buf, *data);

§Extracting the Argon2 parameters in the encrypted data

use abcrypt::{argon2, Encryptor};

let data = b"Hello, world!\n";
let passphrase = "passphrase";

// Encrypt `data` using `passphrase`.
let ciphertext = Encryptor::new(data, passphrase)
    .map(|c| c.encrypt_to_vec())
    .unwrap();

// And extract the Argon2 parameters from it.
let params = abcrypt::Params::new(ciphertext).unwrap();
assert_eq!(params.memory_cost(), argon2::Params::DEFAULT_M_COST);
assert_eq!(params.time_cost(), argon2::Params::DEFAULT_T_COST);
assert_eq!(params.parallelism(), argon2::Params::DEFAULT_P_COST);

Re-exports§

pub use argon2;
pub use blake2;
pub use chacha20poly1305;

Structs§

Argon2
The Argon2 context used for the encrypted data.
Decryptor
Decryptor for the abcrypt encrypted data format.
Encryptor
Encryptor for the abcrypt encrypted data format.
Params
The Argon2 parameters used for the encrypted data.

Enums§

Error
The error type for the abcrypt encrypted data format.

Constants§

HEADER_SIZE
The number of bytes of the header.
TAG_SIZE
The number of bytes of the MAC (authentication tag) of the ciphertext.

Functions§

decryptalloc
Decrypts ciphertext and into a newly allocated Vec.
encryptalloc
Encrypts plaintext and into a newly allocated Vec.
encrypt_with_contextalloc
Encrypts plaintext with the specified Algorithm, Version and Params and into a newly allocated Vec.
encrypt_with_paramsalloc
Encrypts plaintext with the specified Params and into a newly allocated Vec.

Type Aliases§

Result
A specialized Result type for read and write operations for the abcrypt encrypted data format.