sha1 is now an optional feature; base16ct updated; rand + thiserror not tied to patch version anymore

This commit is contained in:
jmic
2025-08-23 21:46:17 +02:00
parent b5e9a20526
commit bba44e21dd
4 changed files with 23 additions and 8 deletions

View File

@@ -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"

View File

@@ -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<AltchaAlgorithm, Self::Err> {
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)

View File

@@ -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),

View File

@@ -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<Sha1>;
type HmacSha256 = Hmac<Sha256>;
type HmacSha384 = Hmac<Sha384>;
type HmacSha512 = Hmac<Sha512>;
pub type ParamsMapType = HashMap<String, String>;
@@ -27,8 +30,10 @@ pub fn random_int(max: u64) -> Result<u64, Error> {
pub fn hash_function(altcha_algorithm: &AltchaAlgorithm, data: &str) -> String {
match altcha_algorithm {
#[cfg(feature = "sha1")]
AltchaAlgorithm::Sha1 => hash_str_to_hex::<Sha1>(data),
AltchaAlgorithm::Sha256 => hash_str_to_hex::<Sha256>(data),
AltchaAlgorithm::Sha384 => hash_str_to_hex::<Sha384>(data),
AltchaAlgorithm::Sha512 => hash_str_to_hex::<Sha512>(data),
}
}
@@ -40,8 +45,10 @@ fn hash_str_to_hex<Hash: Digest>(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::<HmacSha1>(data, key),
AltchaAlgorithm::Sha256 => hmac_from_slice_to_hex_str::<HmacSha256>(data, key),
AltchaAlgorithm::Sha384 => hmac_from_slice_to_hex_str::<HmacSha384>(data, key),
AltchaAlgorithm::Sha512 => hmac_from_slice_to_hex_str::<HmacSha512>(data, key),
}
}