diff --git a/Cargo.toml b/Cargo.toml index 4a564d1..132f31e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,22 +14,23 @@ path = "src/lib.rs" [[example]] name = "server" path = "examples/server.rs" -required-features = ["json"] +required-features = ["json", "sha1"] [dependencies] chrono = "0.4" -rand = "0.9.0" +rand = "0.9" sha2 = "0" -base16ct = { version = "0.2", features = ["alloc"] } -sha1 = "0" +base16ct = { version = "0.3", features = ["alloc"] } +sha1 = { version = "0", optional = true } hmac = "0" serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", optional = true } -thiserror = "2.0.12" +thiserror = "2.0" [features] default = [] -json = ["serde_json"] +json = ["dep:serde_json"] +sha1 = ["dep:sha1"] [dev-dependencies] actix-web = "4" diff --git a/src/algorithm.rs b/src/algorithm.rs index b299e08..0a8e264 100644 --- a/src/algorithm.rs +++ b/src/algorithm.rs @@ -5,10 +5,13 @@ use std::str::FromStr; /// Algorithm options for the challenge #[derive(Debug, Clone, Copy, Deserialize, Serialize)] pub enum AltchaAlgorithm { + #[cfg(feature = "sha1")] #[serde(rename = "SHA-1")] Sha1, #[serde(rename = "SHA-256")] Sha256, + #[serde(rename = "SHA-384")] + Sha384, #[serde(rename = "SHA-512")] Sha512, } @@ -17,8 +20,10 @@ impl FromStr for AltchaAlgorithm { type Err = (); fn from_str(input: &str) -> Result { match input { + #[cfg(feature = "sha1")] "SHA-1" => Ok(AltchaAlgorithm::Sha1), "SHA-256" => Ok(AltchaAlgorithm::Sha256), + "SHA-384" => Ok(AltchaAlgorithm::Sha384), "SHA-512" => Ok(AltchaAlgorithm::Sha512), _ => Err(()), } @@ -28,8 +33,10 @@ impl FromStr for AltchaAlgorithm { impl Display for AltchaAlgorithm { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let str = match self { + #[cfg(feature = "sha1")] AltchaAlgorithm::Sha1 => "SHA-1", AltchaAlgorithm::Sha256 => "SHA-256", + AltchaAlgorithm::Sha384 => "SHA-384", AltchaAlgorithm::Sha512 => "SHA-512", }; write!(f, "{}", str) diff --git a/src/lib.rs b/src/lib.rs index 93ec7e2..2c409fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -345,7 +345,7 @@ mod tests { } #[test] - #[cfg(feature = "json")] + #[cfg(all(feature = "json", feature = "sha1"))] fn test_create_json_challenge() { let challenge_json = create_json_challenge(ChallengeOptions { algorithm: Some(AltchaAlgorithm::Sha1), diff --git a/src/utils.rs b/src/utils.rs index 059d26f..685a98a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,12 +3,15 @@ use hmac::digest::{Digest, KeyInit}; use hmac::{Hmac, Mac}; use rand::distr::uniform::Error; use rand::Rng; +#[cfg(feature = "sha1")] use sha1::Sha1; -use sha2::{Sha256, Sha512}; +use sha2::{Sha256, Sha384, Sha512}; use std::collections::HashMap; +#[cfg(feature = "sha1")] type HmacSha1 = Hmac; type HmacSha256 = Hmac; +type HmacSha384 = Hmac; type HmacSha512 = Hmac; pub type ParamsMapType = HashMap; @@ -27,8 +30,10 @@ pub fn random_int(max: u64) -> Result { pub fn hash_function(altcha_algorithm: &AltchaAlgorithm, data: &str) -> String { match altcha_algorithm { + #[cfg(feature = "sha1")] AltchaAlgorithm::Sha1 => hash_str_to_hex::(data), AltchaAlgorithm::Sha256 => hash_str_to_hex::(data), + AltchaAlgorithm::Sha384 => hash_str_to_hex::(data), AltchaAlgorithm::Sha512 => hash_str_to_hex::(data), } } @@ -40,8 +45,10 @@ fn hash_str_to_hex(data: &str) -> String { pub fn hmac_function(altcha_algorithm: &AltchaAlgorithm, data: &str, key: &str) -> String { match altcha_algorithm { + #[cfg(feature = "sha1")] AltchaAlgorithm::Sha1 => hmac_from_slice_to_hex_str::(data, key), AltchaAlgorithm::Sha256 => hmac_from_slice_to_hex_str::(data, key), + AltchaAlgorithm::Sha384 => hmac_from_slice_to_hex_str::(data, key), AltchaAlgorithm::Sha512 => hmac_from_slice_to_hex_str::(data, key), } }