-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hsh): decoupling argon2i, bcrypt and scrypt
- Loading branch information
1 parent
5db2d05
commit 48f3b99
Showing
7 changed files
with
279 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright © 2023 Hash (HSH) library. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use crate::models::hash_algorithm::HashingAlgorithm; | ||
use argon2rs::argon2i_simple; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
/// Implementation of the Argon2i hashing algorithm. | ||
/// | ||
/// `Argon2i` is a struct that represents the Argon2i hashing algorithm, | ||
/// which is a memory-hard algorithm resistant to GPU-based attacks and side-channel attacks. | ||
/// It is one of the multiple hashing algorithms that can be used for password hashing in this library. | ||
/// | ||
/// This struct implements the `HashingAlgorithm` trait, providing a concrete implementation | ||
/// for hashing passwords using the Argon2i algorithm. | ||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)] | ||
pub struct Argon2i; | ||
|
||
impl HashingAlgorithm for Argon2i { | ||
/// Hashes a given password using Argon2i algorithm. | ||
/// | ||
/// Given a plaintext `password` and a `salt`, this method returns a hashed representation | ||
/// of the password using Argon2i algorithm. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `password`: The plaintext password to be hashed. | ||
/// - `salt`: A cryptographic salt to prevent rainbow table attacks. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` containing the hashed password as a vector of bytes. | ||
/// If hashing fails for some reason, returns a `String` detailing the error. | ||
fn hash_password(password: &str, salt: &str) -> Result<Vec<u8>, String> { | ||
Ok(argon2i_simple(password, salt).into_iter().collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright © 2023 Hash (HSH) library. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use crate::models::hash_algorithm::HashingAlgorithm; | ||
use bcrypt::{hash, DEFAULT_COST}; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
/// Implementation of the Bcrypt hashing algorithm. | ||
/// | ||
/// `Bcrypt` is a struct that represents the Bcrypt hashing algorithm, | ||
/// which is based on the Blowfish cipher and is particularly effective against brute-force attacks. | ||
/// | ||
/// This struct implements the `HashingAlgorithm` trait, providing a concrete implementation | ||
/// for hashing passwords using the Bcrypt algorithm. | ||
/// | ||
/// # Features | ||
/// | ||
/// - Computationally intensive, making brute-force attacks more difficult. | ||
/// - Uses key stretching to make pre-computed attacks (like rainbow tables) less effective. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use hsh::models::hash_algorithm::HashingAlgorithm; | ||
/// use hsh::algorithms::bcrypt::Bcrypt; | ||
/// | ||
/// let password = "supersecret"; | ||
/// let salt = "randomsalt"; | ||
/// | ||
/// let hashed_password = Bcrypt::hash_password(password, salt).unwrap(); | ||
/// ``` | ||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)] | ||
pub struct Bcrypt; | ||
|
||
impl HashingAlgorithm for Bcrypt { | ||
/// Hashes a given password using the Bcrypt algorithm. | ||
/// | ||
/// Given a plaintext `password` and a `salt`, this method returns a hashed representation | ||
/// of the password using the Bcrypt algorithm. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `password`: The plaintext password to be hashed. | ||
/// - `salt`: A cryptographic salt to prevent rainbow table attacks. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` containing the hashed password as a vector of bytes. | ||
/// If hashing fails for some reason, returns a `String` detailing the error. | ||
fn hash_password(password: &str, _salt: &str) -> Result<Vec<u8>, String> { | ||
hash(password, DEFAULT_COST) | ||
.map_err(|e| e.to_string()) | ||
.map(|hash_parts| hash_parts.into_bytes()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright © 2023 Hash (HSH) library. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
/// The `argon2i` module contains the Argon2i password hashing algorithm. | ||
pub mod argon2i; | ||
|
||
/// The `bcrypt` module contains the Bcrypt password hashing algorithm. | ||
pub mod bcrypt; | ||
|
||
/// The `scrypt` module contains the Scrypt password hashing algorithm. | ||
pub mod scrypt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright © 2023 Hash (HSH) library. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use crate::models::hash_algorithm::HashingAlgorithm; | ||
use scrypt::scrypt; | ||
use scrypt::Params; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
/// Implementation of the Scrypt hashing algorithm. | ||
/// | ||
/// `Scrypt` is a struct that represents the Scrypt hashing algorithm, | ||
/// which is a memory-hard algorithm designed to be computationally intensive, | ||
/// thereby making it difficult to perform large-scale custom hardware attacks. | ||
/// | ||
/// This struct implements the `HashingAlgorithm` trait, providing a concrete implementation | ||
/// for hashing passwords using the Scrypt algorithm. | ||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)] | ||
pub struct Scrypt; | ||
|
||
impl HashingAlgorithm for Scrypt { | ||
/// Hashes a given password using the Scrypt algorithm. | ||
/// | ||
/// Given a plaintext `password` and a `salt`, this method returns a hashed representation | ||
/// of the password using the Scrypt algorithm. | ||
/// | ||
/// # Parameters | ||
/// | ||
/// - `password`: The plaintext password to be hashed. | ||
/// - `salt`: A cryptographic salt to prevent rainbow table attacks. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a `Result` containing the hashed password as a vector of bytes. | ||
/// If hashing fails for some reason, it returns a `String` detailing the error. | ||
fn hash_password(password: &str, salt: &str) -> Result<Vec<u8>, String> { | ||
let params = Params::new(14, 8, 1, 64).map_err(|e| e.to_string())?; | ||
let mut output = [0u8; 64]; | ||
scrypt( | ||
password.as_bytes(), | ||
salt.as_bytes(), | ||
¶ms, | ||
&mut output, | ||
) | ||
.map_err(|e| e.to_string()) | ||
.map(|_| output.to_vec()) | ||
} | ||
} |
Oops, something went wrong.