Crate abcrypt

source ·
Expand description

The abcrypt crate is an implementation of the abcrypt encrypted data 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::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 cipher = Encryptor::with_params(data, passphrase, params).unwrap();
let mut buf = [u8::default(); 170];
cipher.encrypt(&mut buf);
assert_ne!(buf, data.as_slice());

// 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.as_slice());

§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§

Structs§

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

Enums§

  • The error type for the abcrypt encrypted data format.

Constants§

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

Functions§

Type Aliases§

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