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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cas-lib"
version = "0.2.68"
version = "0.2.69"
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 Expand Up @@ -35,7 +35,7 @@ chacha20poly1305 = "0.10.1"
slh-dsa = "0.0.3"
ml-kem = "0.2.1"
reqwest = { version = "0.12.24", features = ["json", "cookies", "rustls-tls"] }
serde = "1.0.228"
serde = { version = "1.0.228", features = ["derive"] }
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread"] }
url = "2.5.7"
once_cell = "1.21.3"
Expand Down
4 changes: 2 additions & 2 deletions src/http/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::Serialize;
use serde::{Deserialize, Serialize};

pub mod runtime;

Expand All @@ -10,7 +10,7 @@ pub struct BenchmarkRequest {
pub time_in_milliseconds: i64
}

#[derive(serde::Deserialize)]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthResponse {
pub token: String,
Expand Down
8 changes: 3 additions & 5 deletions src/password_hashers/bcrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

use bcrypt::{hash, verify, DEFAULT_COST};

use super::cas_password_hasher::CASPasswordHasher;

pub struct CASBCrypt;

impl CASPasswordHasher for CASBCrypt {
impl CASBCrypt {
/// Hashes a password using bcrypt.
/// Returns the hashed password as a string.
fn hash_password(password_to_hash: String) -> String {
pub fn hash_password(password_to_hash: String) -> String {
return hash(password_to_hash, DEFAULT_COST).unwrap();
}

/// Verifies a password against a hashed password using bcrypt.
/// Returns true if the password matches the hashed password, false otherwise.
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
pub fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
return verify(password_to_verify, &hashed_password).unwrap();
}
}
5 changes: 0 additions & 5 deletions src/password_hashers/cas_password_hasher.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
pub trait CASPasswordHasher {
fn hash_password(password_to_hash: String) -> String;
fn verify_password(hashed_password: String, password_to_verify: String) -> bool;
}

pub struct Pbkdf2Result {
pub password: Vec<u8>,
pub salt: Vec<u8>
Expand Down
22 changes: 16 additions & 6 deletions src/password_hashers/scrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@

use scrypt::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Scrypt,
Scrypt, Params
};

use super::cas_password_hasher::CASPasswordHasher;

pub struct CASScrypt;

impl CASPasswordHasher for CASScrypt {
impl CASScrypt {
/// Hashes a passwith using Scrypt with custom params.
/// Parameters:
/// - password_to_hash: The password to be hashed.
/// - cpu_memory_cost: log₂ of the Scrypt parameter `N`, the work factor.
/// - block_size: `r` parameter: resource usage.
/// - parallelism: `p` parameter: parallelization.
pub fn hash_password_customized(password_to_hash: String, cpu_memory_cost: u8, block_size: u32, parallelism: u32) -> String {
let salt = SaltString::generate(&mut OsRng);
let params = Params::new(cpu_memory_cost, block_size, parallelism, 32).unwrap();
return Scrypt.hash_password_customized(password_to_hash.as_bytes(), None, None, params, &salt).unwrap().to_string();
}

/// Hashes a password using Scrypt.
/// Returns the hashed password as a string.
fn hash_password(password_to_hash: String) -> String {
pub fn hash_password(password_to_hash: String) -> String {
let salt = SaltString::generate(&mut OsRng);
return Scrypt
.hash_password(password_to_hash.as_bytes(), &salt)
Expand All @@ -24,7 +34,7 @@ impl CASPasswordHasher for CASScrypt {

/// Verifies a password against a hashed password using Scrypt.
/// Returns true if the password matches the hashed password, false otherwise.
fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
pub fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
let parsed_hash = PasswordHash::new(&hashed_password).unwrap();
return Scrypt
.verify_password(password_to_verify.as_bytes(), &parsed_hash)
Expand Down
10 changes: 9 additions & 1 deletion tests/password_hashers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
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}}};
use cas_lib::{password_hashers::{argon2::CASArgon, bcrypt::CASBCrypt, scrypt::CASScrypt}, symmetric::{aes::{CASAES128, CASAES256}, cas_symmetric_encryption::{CASAES128Encryption, CASAES256Encryption}}};

#[test]
pub fn argon2_hash_with_parameters() {
Expand Down Expand Up @@ -53,6 +53,14 @@ mod password_hashers {
assert_eq!(true, verification);
}

#[test]
pub fn scrypt_hash_password_customized() {
let password = "DoNotUseThisPassword".to_string();
let hash = CASScrypt::hash_password_customized(password.clone(), 17, 8, 1);
let verification = CASScrypt::verify_password(hash, password);
assert_eq!(true, verification);
}

#[test]
pub fn bcrypt_hash_password() {
let password = "DoNotUseThisPassword".to_string();
Expand Down