Cryptographic-API-Services / cas-lib

A rust crate that provides wrapper functions of the RustCrypto suite for the cryptographic algorithms.
Apache License 2.0
1 stars 1 forks source link

Add PBKDF2 #12

Closed WingZer0o closed 4 days ago

WingZer0o commented 5 days ago

This is something that i have been meaning to get around too. This will be useful for "master" password derivations.

https://docs.rs/pbkdf2/latest/pbkdf2/ https://github.com/RustCrypto/password-hashes/tree/master/pbkdf2

use cas_lib::{password_hashers::{argon2::CASArgon, cas_password_hasher::CASPasswordHasher}, symmetric::{aes::CASAES256, cas_symmetric_encryption::CASAESEncryption}};
use pbkdf2::pbkdf2_hmac_array;
use sha2::Sha256;

fn main()  {
    let bad_password = CASArgon::hash_password("welcome home".to_string());
    let bad_password_bytes = bad_password.as_bytes();

    let salt = b"salt";
    // number of iterations
    let n = 600_000;

    let key = pbkdf2_hmac_array::<Sha256, 32>(bad_password_bytes, salt, n).to_vec();

    let nonce = CASAES256::generate_nonce();
    let data_to_encrypt = "welcome home".as_bytes().to_vec();

    let encrypted = CASAES256::encrypt_plaintext(key.clone(), nonce.clone(), data_to_encrypt.clone());
    let decrypted = CASAES256::decrypt_ciphertext(key, nonce, encrypted);
    assert_eq!(decrypted, data_to_encrypt)
}