chore(sha): comment code and update sha tests
This commit is contained in:
parent
b04635a3ea
commit
b5c8e34cba
|
@ -0,0 +1,12 @@
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
/// Custom error type
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum Error {
|
||||||
|
/// Invalid HMAC Key
|
||||||
|
#[error("invalid key")]
|
||||||
|
InvalidKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Alias to a `Resuly<T, Error>` with the cutom [enum@Error].
|
||||||
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
@ -1,18 +1,8 @@
|
||||||
use hmac::{Hmac, Mac};
|
use hmac::{Hmac, Mac};
|
||||||
use sha1::Sha1;
|
use sha1::Sha1;
|
||||||
use sha2::{Sha256, Sha512};
|
use sha2::{Sha256, Sha512};
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
/// Custom error type
|
use super::{Error, Result};
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum Error {
|
|
||||||
/// Invalid HMAC Key
|
|
||||||
#[error("invalid key")]
|
|
||||||
InvalidKey,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Alias to a `Resuly<T, Error>` with the cutom [Error].
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
|
||||||
|
|
||||||
/// HMAC hashing algorithms
|
/// HMAC hashing algorithms
|
||||||
pub enum AlgorithmMac {
|
pub enum AlgorithmMac {
|
||||||
|
@ -25,6 +15,38 @@ pub enum AlgorithmMac {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute cryptographic hash from bytes (HMAC Sha1, HMAC Sha256, HMAC Sha512).
|
/// Compute cryptographic hash from bytes (HMAC Sha1, HMAC Sha256, HMAC Sha512).
|
||||||
|
///
|
||||||
|
/// ## Method 1 (recommend)
|
||||||
|
/// ```
|
||||||
|
/// use crypto_utils::sha::{AlgorithmMac, CryptographicMac};
|
||||||
|
///
|
||||||
|
/// // compute hash
|
||||||
|
/// let hash_bytes: Vec<u8> = CryptographicMac::hash(AlgorithmMac::HmacSHA1, b"secret", b"input").unwrap();
|
||||||
|
///
|
||||||
|
/// // decode hash to a String
|
||||||
|
/// let hash: String = hex::encode(hash_bytes);
|
||||||
|
///
|
||||||
|
/// # assert_eq!(hash, "30440f36ddc2809bbd4c8b1f37a6e80d7588c303".to_string())
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## Method 2
|
||||||
|
/// ```
|
||||||
|
/// use crypto_utils::sha::{AlgorithmMac, CryptographicMac};
|
||||||
|
///
|
||||||
|
/// // create a new hasher
|
||||||
|
/// let mut hasher = CryptographicMac::new(AlgorithmMac::HmacSHA1, b"secret").unwrap();
|
||||||
|
///
|
||||||
|
/// // set value in hasher
|
||||||
|
/// hasher.update(b"input");
|
||||||
|
///
|
||||||
|
/// // compute hash
|
||||||
|
/// let hash_bytes: Vec<u8> = hasher.finalize();
|
||||||
|
///
|
||||||
|
/// // decode hash to a String
|
||||||
|
/// let hash: String = hex::encode(hash_bytes);
|
||||||
|
///
|
||||||
|
/// # assert_eq!(hash, "30440f36ddc2809bbd4c8b1f37a6e80d7588c303".to_string())
|
||||||
|
/// ```
|
||||||
pub enum CryptographicMac {
|
pub enum CryptographicMac {
|
||||||
/// HMAC Sha1 hasher
|
/// HMAC Sha1 hasher
|
||||||
HmacSha1(Hmac<Sha1>),
|
HmacSha1(Hmac<Sha1>),
|
||||||
|
|
|
@ -1,21 +1,49 @@
|
||||||
//! Module for creating sha1, sha256 and sha512 hashes.
|
//! Module for creating sha1, sha256 and sha512 hashes.
|
||||||
//!
|
//!
|
||||||
|
//! ## Sha
|
||||||
|
//!
|
||||||
|
//! Example of computing a sha1, sha256 and sha512 hashes:
|
||||||
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! use crypto_utils::sha::{Algorithm, CryptographicHash};
|
//! use crypto_utils::sha::{Algorithm, CryptographicHash};
|
||||||
//!
|
//!
|
||||||
//! // sha1
|
//! // Sha1
|
||||||
//! CryptographicHash::hash(Algorithm::SHA1, b"P@ssw0rd");
|
//! let hash: Vec<u8> = CryptographicHash::hash(Algorithm::SHA1, b"input");
|
||||||
//!
|
//!
|
||||||
//! // sha256
|
//! // Sha256
|
||||||
//! CryptographicHash::hash(Algorithm::SHA256, b"P@ssw0rd");
|
//! let hash: Vec<u8> = CryptographicHash::hash(Algorithm::SHA256, b"input");
|
||||||
//!
|
//!
|
||||||
//! // sha512
|
//! // Sha512
|
||||||
//! CryptographicHash::hash(Algorithm::SHA512, b"P@ssw0rd");
|
//! let hash: Vec<u8> = CryptographicHash::hash(Algorithm::SHA512, b"input");
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ## HMAC-Sha
|
||||||
|
//!
|
||||||
|
//! Read about HMAC in [wikipedia](https://en.wikipedia.org/wiki/HMAC)
|
||||||
|
//!
|
||||||
|
//! Example of computing a HMAC hashes (sha1, sha256 and sha512):
|
||||||
|
//!
|
||||||
|
//! ```no_run
|
||||||
|
//! use crypto_utils::sha::{AlgorithmMac, CryptographicMac};
|
||||||
|
//!
|
||||||
|
//! // secret value
|
||||||
|
//! const SECRET: &[u8] = b"secret";
|
||||||
|
//!
|
||||||
|
//! // HMAC Sha1
|
||||||
|
//! let hash: Vec<u8> = CryptographicMac::hash(AlgorithmMac::HmacSHA1, SECRET, b"input").unwrap();
|
||||||
|
//!
|
||||||
|
//! // HMAC Sha256
|
||||||
|
//! let hash: Vec<u8> = CryptographicMac::hash(AlgorithmMac::HmacSHA256, SECRET, b"input").unwrap();
|
||||||
|
//!
|
||||||
|
//! // HMAC Sha512
|
||||||
|
//! let hash: Vec<u8> = CryptographicMac::hash(AlgorithmMac::HmacSHA512, SECRET, b"input").unwrap();
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
mod error;
|
||||||
mod mac;
|
mod mac;
|
||||||
#[allow(clippy::module_inception)]
|
#[allow(clippy::module_inception)]
|
||||||
mod sha;
|
mod sha;
|
||||||
|
|
||||||
|
pub use error::*;
|
||||||
pub use mac::*;
|
pub use mac::*;
|
||||||
pub use sha::*;
|
pub use sha::*;
|
||||||
|
|
|
@ -14,17 +14,17 @@ pub enum Algorithm {
|
||||||
|
|
||||||
/// Compute cryptographic hash from bytes (sha1, sha256, sha512).
|
/// Compute cryptographic hash from bytes (sha1, sha256, sha512).
|
||||||
///
|
///
|
||||||
/// Method 1
|
/// Method 1 (recommend)
|
||||||
/// ```
|
/// ```
|
||||||
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
|
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
|
||||||
///
|
///
|
||||||
/// // compute hash
|
/// // compute hash
|
||||||
/// let hash_bytes = CryptographicHash::hash(Algorithm::SHA1, b"P@ssw0rd");
|
/// let hash_bytes: Vec<u8> = CryptographicHash::hash(Algorithm::SHA1, b"P@ssw0rd");
|
||||||
///
|
///
|
||||||
/// // decode hash to a String
|
/// // decode hash to a String
|
||||||
/// let hash = hex::encode(hash_bytes);
|
/// let hash: String = hex::encode(hash_bytes);
|
||||||
///
|
///
|
||||||
/// assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
|
/// # assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Method 2
|
/// Method 2
|
||||||
|
@ -32,18 +32,18 @@ pub enum Algorithm {
|
||||||
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
|
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
|
||||||
///
|
///
|
||||||
/// // create a new hasher
|
/// // create a new hasher
|
||||||
/// let mut sha1 = CryptographicHash::new(Algorithm::SHA1);
|
/// let mut hasher = CryptographicHash::new(Algorithm::SHA1);
|
||||||
///
|
///
|
||||||
/// // set value in hasher
|
/// // set value in hasher
|
||||||
/// sha1.update(b"P@ssw0rd");
|
/// hasher.update(b"P@ssw0rd");
|
||||||
///
|
///
|
||||||
/// // compute hash
|
/// // compute hash
|
||||||
/// let hash_bytes = sha1.finalize();
|
/// let hash_bytes: Vec<u8> = hasher.finalize();
|
||||||
///
|
///
|
||||||
/// // decode hash to a String
|
/// // decode hash to a String
|
||||||
/// let hash = hex::encode(hash_bytes);
|
/// let hash: String = hex::encode(hash_bytes);
|
||||||
///
|
///
|
||||||
/// assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
|
/// # assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum CryptographicHash {
|
pub enum CryptographicHash {
|
||||||
|
@ -56,7 +56,7 @@ pub enum CryptographicHash {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CryptographicHash {
|
impl CryptographicHash {
|
||||||
/// Create a new SHA hasher
|
/// Create a new Sha hasher
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
|
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
|
||||||
|
@ -152,53 +152,51 @@ impl CryptographicHash {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{Algorithm, CryptographicHash};
|
use super::{Algorithm, CryptographicHash};
|
||||||
|
|
||||||
|
const INPUT: &[u8] = b"input";
|
||||||
|
|
||||||
|
// expected hashes
|
||||||
|
const EXPECTED_SHA1: &str = "140f86aae51ab9e1cda9b4254fe98a74eb54c1a1";
|
||||||
|
const EXPECTED_SHA256: &str =
|
||||||
|
"c96c6d5be8d08a12e7b5cdc1b207fa6b2430974c86803d8891675e76fd992c20";
|
||||||
|
const EXPECTED_SHA512: &str =
|
||||||
|
"dc6d6c30f2be9c976d6318c9a534d85e9a1c3f3608321a04b4678ef408124d45d7164f3e562e68c6c0b6c077340a785824017032fddfa924f4cf400e6cbb6adc";
|
||||||
|
|
||||||
/// Test a Sha1 hasher
|
/// Test a Sha1 hasher
|
||||||
#[test]
|
#[test]
|
||||||
fn sha1() {
|
fn sha1() {
|
||||||
// expected hash
|
|
||||||
let expected_hash = "7726bd9560e1ad4a1a4f056cae5c0c9ea8bacfc2".to_string();
|
|
||||||
|
|
||||||
// compute hash
|
// compute hash
|
||||||
let hash_bytes = CryptographicHash::hash(Algorithm::SHA1, b"test sha1 hash");
|
let hash_bytes = CryptographicHash::hash(Algorithm::SHA1, INPUT);
|
||||||
|
|
||||||
// decode hash to a String
|
// decode hash to a String
|
||||||
let hash = hex::encode(hash_bytes);
|
let hash = hex::encode(hash_bytes);
|
||||||
|
|
||||||
// validate hash
|
// validate hash
|
||||||
assert_eq!(hash, expected_hash)
|
assert_eq!(hash, EXPECTED_SHA1.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test a Sha256 hasher
|
/// Test a Sha256 hasher
|
||||||
#[test]
|
#[test]
|
||||||
fn sha256() {
|
fn sha256() {
|
||||||
// expected hash
|
|
||||||
let expected_hash =
|
|
||||||
"eaf6e4198f39ccd63bc3e957d43bf4ef67f12c318c8e3cdc2567a37339902dac".to_string();
|
|
||||||
|
|
||||||
// compute hash
|
// compute hash
|
||||||
let hash_bytes = CryptographicHash::hash(Algorithm::SHA256, b"test sha256 hash");
|
let hash_bytes = CryptographicHash::hash(Algorithm::SHA256, INPUT);
|
||||||
|
|
||||||
// decode hash to a String
|
// decode hash to a String
|
||||||
let hash = hex::encode(hash_bytes);
|
let hash = hex::encode(hash_bytes);
|
||||||
|
|
||||||
// validate hash
|
// validate hash
|
||||||
assert_eq!(hash, expected_hash)
|
assert_eq!(hash, EXPECTED_SHA256.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test a Sha512 hasher
|
/// Test a Sha512 hasher
|
||||||
#[test]
|
#[test]
|
||||||
fn sha512() {
|
fn sha512() {
|
||||||
// expected hash
|
|
||||||
let expected_hash =
|
|
||||||
"b43b4d7178014c92f55be828d66c9f98211fc67b385f7790a5b4b2fcb89fe1831645b5a4c17f3f7f11d8f34d2800a77a2b8faa5a0fb9d6b8f7befbc29a9ce795".to_string();
|
|
||||||
|
|
||||||
// compute hash
|
// compute hash
|
||||||
let hash_bytes = CryptographicHash::hash(Algorithm::SHA512, b"test sha512 hash");
|
let hash_bytes = CryptographicHash::hash(Algorithm::SHA512, INPUT);
|
||||||
|
|
||||||
// decode hash to a String
|
// decode hash to a String
|
||||||
let hash = hex::encode(hash_bytes);
|
let hash = hex::encode(hash_bytes);
|
||||||
|
|
||||||
// validate hash
|
// validate hash
|
||||||
assert_eq!(hash, expected_hash)
|
assert_eq!(hash, EXPECTED_SHA512.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue