Crate scryptenc

Source
Expand description

The scryptenc crate is an implementation of the scrypt encrypted data format.

This crate supports the scrypt version 0 file format.

§Examples

§Encryption and decryption

use scryptenc::{scrypt::Params, Decryptor, Encryptor};

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

// Encrypt `data` using `passphrase`.
let params = Params::new(10, 8, 1, Params::RECOMMENDED_LEN).unwrap();
let ciphertext = Encryptor::with_params(data, passphrase, params).encrypt_to_vec();
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.

use scryptenc::{scrypt::Params, Decryptor, Encryptor};

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

// Encrypt `data` using `passphrase`.
let params = Params::new(10, 8, 1, Params::RECOMMENDED_LEN).unwrap();
let cipher = Encryptor::with_params(data, passphrase, params);
let mut buf = [u8::default(); 142];
cipher.encrypt(&mut buf);
assert_ne!(buf.as_slice(), data);

// 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 scrypt parameters in the encrypted data

use scryptenc::{scrypt, Encryptor};

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

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

// And extract the scrypt parameters from it.
let params = scryptenc::Params::new(ciphertext).unwrap();
assert_eq!(params.log_n(), scrypt::Params::RECOMMENDED_LOG_N);
assert_eq!(params.n(), 1 << scrypt::Params::RECOMMENDED_LOG_N);
assert_eq!(params.r(), scrypt::Params::RECOMMENDED_R);
assert_eq!(params.p(), scrypt::Params::RECOMMENDED_P);

Re-exports§

pub use hmac;
pub use scrypt;

Structs§

Decryptor
Decryptor for the scrypt encrypted data format.
Encryptor
Encryptor for the scrypt encrypted data format.
Params
The scrypt parameters used for the encrypted data.

Enums§

Error
The error type for the scrypt 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 scrypt encrypted data format.

Functions§

decryptalloc
Decrypts ciphertext and into a newly allocated Vec.
encryptalloc
Encrypts plaintext 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 scrypt encrypted data format.