pub struct PassKey { /* private fields */ }Expand description
Key and Salt that are used to encrypt/decrypt data.
Implementations§
Source§impl PassKey
impl PassKey
Sourcepub fn from_passphrase(passphrase: &[u8]) -> Result<PassKey, KeyDerivationError>
pub fn from_passphrase(passphrase: &[u8]) -> Result<PassKey, KeyDerivationError>
Create a new PassKey with a random Salt.
Note that passphrase memory is not being zeroed after it has been
used. Code that provides passphrase should take care of zeroing that
memory.
Can fail for the same reasons as PassKey::with_salt()
(./struct.PassKey.html#method.with_salt), that is:
- passphrase is empty
- deriving key failed (can happen due to OOM)
E.g.
use tox_encryptsave::*;
// fails with an empty passphrase
assert_eq!(PassKey::from_passphrase(&[]), Err(KeyDerivationError::Null));Sourcepub fn with_salt(
passphrase: &[u8],
salt: Salt,
) -> Result<PassKey, KeyDerivationError>
pub fn with_salt( passphrase: &[u8], salt: Salt, ) -> Result<PassKey, KeyDerivationError>
Create a new PassKey with provided Salt, rather than using a random
one.
Note that passphrase memory is not being zeroed after it has been
used. Code that provides passphrase should take care of zeroing that
memory.
§Fails when:
* passphrase is empty
* deriving key failed (can happen due to OOM)E.g.
use tox_crypto::pwhash::gen_salt;
use tox_encryptsave::*;
assert_eq!(PassKey::with_salt(&[], gen_salt()), Err(KeyDerivationError::Null));Sourcepub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>, EncryptionError>
pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>, EncryptionError>
Encrypts provided data with self PassKey.
Encrypted data is bigger than supplied data by EXTRA_LENGTH
(./constant.EXTRA_LENGTH.html).
§Fails when:
- provided
datais empty
E.g.
use tox_encryptsave::*;
// ↓ don't
let passkey = PassKey::from_passphrase(&[0]).expect("Failed to unwrap PassKey!");
assert_eq!(passkey.encrypt(&[]), Err(EncryptionError::Null));Sourcepub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>, DecryptionError>
pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>, DecryptionError>
Decrypts provided data with self PassKey.
Decrypted data is smaller by EXTRA_LENGTH
than encrypted data.
§Fails when:
- provided
datais empty - size of provided
datais less thanEXTRA_LENGTH - format of provided
datais wrong - decrypting
datafails- could be due to OOM or by providing bytes that aren’t encrypted after encrypted part
E.g.
use tox_encryptsave::*;
// ↓ don't
let passkey = PassKey::from_passphrase(&[0]).expect("Failed to unwrap PassKey!");
// empty data
assert_eq!(passkey.decrypt(&[]), Err(DecryptionError::Null));