Expand description
The scryptenc
crate is an implementation of the scrypt encrypted data
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, 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 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§
Structs§
- Decryptor for the scrypt encrypted data format.
- Encryptor for the scrypt encrypted data format.
- The scrypt parameters used for the encrypted data.
Enums§
- The error type for the scrypt encrypted data format.
Constants§
- The number of bytes of the header.
- The number of bytes of the MAC (authentication tag) of the scrypt encrypted data format.
Functions§
- decrypt
alloc
Decryptsciphertext
and into a newly allocatedVec
. - encrypt
alloc
Encryptsplaintext
and into a newly allocatedVec
. - encrypt_
with_ params alloc
Type Aliases§
- A specialized
Result
type for read and write operations for the scrypt encrypted data format.