Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cas-lib"
version = "0.2.67"
version = "0.2.68"
edition = "2021"
description = "A function wrapper layer for RustCrypto and Dalek-Cryptography. Intended to be used in FFI situations with a global heap deallactor at the top level project."
license = "Apache-2.0"
Expand Down
16 changes: 15 additions & 1 deletion src/password_hashers/argon2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use argon2::{
password_hash::{rand_core::OsRng, SaltString},
Argon2, PasswordHash, PasswordHasher, PasswordVerifier,
Argon2, PasswordHash, PasswordHasher, PasswordVerifier, Params
};
use rand::RngCore;

Expand All @@ -9,6 +9,20 @@ pub struct CASArgon;

impl CASArgon {

/// Hashes a password using Argon2 with custom parameters.
/// Returns the hashed password as a string.
/// Parameters:
/// - memory_cost: Memory cost in kibibytes.
/// - iterations: Number of iterations.
/// - parallelism: Degree of parallelism.
/// - password_to_hash: The password to be hashed.
pub fn hash_password_parameters(memory_cost: u32, iterations: u32, parallelism: u32, password_to_hash: String) -> String {
let params = Params::new(memory_cost * 1024, iterations, parallelism, None).unwrap();
let argon2 = Argon2::new(argon2::Algorithm::Argon2id, argon2::Version::V0x13, params);
let salt = SaltString::generate(&mut OsRng);
let hash = argon2.hash_password(password_to_hash.as_bytes(), &salt).unwrap();
hash.to_string()
}
/// Derives a 128-bit AES key from a password using Argon2.
/// Returns the derived key as a vector of bytes.
pub fn derive_aes_128_key(password: Vec<u8>) -> Vec<u8> {
Expand Down
8 changes: 8 additions & 0 deletions tests/password_hashers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ mod password_hashers {
use std::path::Path;
use cas_lib::{password_hashers::{argon2::CASArgon, bcrypt::CASBCrypt, cas_password_hasher::CASPasswordHasher, scrypt::CASScrypt}, symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::{CASAES128Encryption, CASAES256Encryption}}};

#[test]
pub fn argon2_hash_with_parameters() {
let password = "BadPassword".to_string();
let hash = CASArgon::hash_password_parameters(1024, 5, 5, password.clone());
let verification = CASArgon::verify_password(hash, password);
assert_eq!(true, verification);
}

#[test]
pub fn argon2_derive_aes_128_and_encrypt() {
let password = b"BadPassword".to_vec(); // do not use this as a password.
Expand Down