Skip to content

Commit bba44e2

Browse files
committed
sha1 is now an optional feature; base16ct updated; rand + thiserror not tied to patch version anymore
1 parent b5e9a20 commit bba44e2

4 files changed

Lines changed: 23 additions & 8 deletions

File tree

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ path = "src/lib.rs"
1414
[[example]]
1515
name = "server"
1616
path = "examples/server.rs"
17-
required-features = ["json"]
17+
required-features = ["json", "sha1"]
1818

1919
[dependencies]
2020
chrono = "0.4"
21-
rand = "0.9.0"
21+
rand = "0.9"
2222
sha2 = "0"
23-
base16ct = { version = "0.2", features = ["alloc"] }
24-
sha1 = "0"
23+
base16ct = { version = "0.3", features = ["alloc"] }
24+
sha1 = { version = "0", optional = true }
2525
hmac = "0"
2626
serde = { version = "1.0", features = ["derive"] }
2727
serde_json = { version = "1.0", optional = true }
28-
thiserror = "2.0.12"
28+
thiserror = "2.0"
2929

3030
[features]
3131
default = []
32-
json = ["serde_json"]
32+
json = ["dep:serde_json"]
33+
sha1 = ["dep:sha1"]
3334

3435
[dev-dependencies]
3536
actix-web = "4"

src/algorithm.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ use std::str::FromStr;
55
/// Algorithm options for the challenge
66
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
77
pub enum AltchaAlgorithm {
8+
#[cfg(feature = "sha1")]
89
#[serde(rename = "SHA-1")]
910
Sha1,
1011
#[serde(rename = "SHA-256")]
1112
Sha256,
13+
#[serde(rename = "SHA-384")]
14+
Sha384,
1215
#[serde(rename = "SHA-512")]
1316
Sha512,
1417
}
@@ -17,8 +20,10 @@ impl FromStr for AltchaAlgorithm {
1720
type Err = ();
1821
fn from_str(input: &str) -> Result<AltchaAlgorithm, Self::Err> {
1922
match input {
23+
#[cfg(feature = "sha1")]
2024
"SHA-1" => Ok(AltchaAlgorithm::Sha1),
2125
"SHA-256" => Ok(AltchaAlgorithm::Sha256),
26+
"SHA-384" => Ok(AltchaAlgorithm::Sha384),
2227
"SHA-512" => Ok(AltchaAlgorithm::Sha512),
2328
_ => Err(()),
2429
}
@@ -28,8 +33,10 @@ impl FromStr for AltchaAlgorithm {
2833
impl Display for AltchaAlgorithm {
2934
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3035
let str = match self {
36+
#[cfg(feature = "sha1")]
3137
AltchaAlgorithm::Sha1 => "SHA-1",
3238
AltchaAlgorithm::Sha256 => "SHA-256",
39+
AltchaAlgorithm::Sha384 => "SHA-384",
3340
AltchaAlgorithm::Sha512 => "SHA-512",
3441
};
3542
write!(f, "{}", str)

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ mod tests {
345345
}
346346

347347
#[test]
348-
#[cfg(feature = "json")]
348+
#[cfg(all(feature = "json", feature = "sha1"))]
349349
fn test_create_json_challenge() {
350350
let challenge_json = create_json_challenge(ChallengeOptions {
351351
algorithm: Some(AltchaAlgorithm::Sha1),

src/utils.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ use hmac::digest::{Digest, KeyInit};
33
use hmac::{Hmac, Mac};
44
use rand::distr::uniform::Error;
55
use rand::Rng;
6+
#[cfg(feature = "sha1")]
67
use sha1::Sha1;
7-
use sha2::{Sha256, Sha512};
8+
use sha2::{Sha256, Sha384, Sha512};
89
use std::collections::HashMap;
910

11+
#[cfg(feature = "sha1")]
1012
type HmacSha1 = Hmac<Sha1>;
1113
type HmacSha256 = Hmac<Sha256>;
14+
type HmacSha384 = Hmac<Sha384>;
1215
type HmacSha512 = Hmac<Sha512>;
1316
pub type ParamsMapType = HashMap<String, String>;
1417

@@ -27,8 +30,10 @@ pub fn random_int(max: u64) -> Result<u64, Error> {
2730

2831
pub fn hash_function(altcha_algorithm: &AltchaAlgorithm, data: &str) -> String {
2932
match altcha_algorithm {
33+
#[cfg(feature = "sha1")]
3034
AltchaAlgorithm::Sha1 => hash_str_to_hex::<Sha1>(data),
3135
AltchaAlgorithm::Sha256 => hash_str_to_hex::<Sha256>(data),
36+
AltchaAlgorithm::Sha384 => hash_str_to_hex::<Sha384>(data),
3237
AltchaAlgorithm::Sha512 => hash_str_to_hex::<Sha512>(data),
3338
}
3439
}
@@ -40,8 +45,10 @@ fn hash_str_to_hex<Hash: Digest>(data: &str) -> String {
4045

4146
pub fn hmac_function(altcha_algorithm: &AltchaAlgorithm, data: &str, key: &str) -> String {
4247
match altcha_algorithm {
48+
#[cfg(feature = "sha1")]
4349
AltchaAlgorithm::Sha1 => hmac_from_slice_to_hex_str::<HmacSha1>(data, key),
4450
AltchaAlgorithm::Sha256 => hmac_from_slice_to_hex_str::<HmacSha256>(data, key),
51+
AltchaAlgorithm::Sha384 => hmac_from_slice_to_hex_str::<HmacSha384>(data, key),
4552
AltchaAlgorithm::Sha512 => hmac_from_slice_to_hex_str::<HmacSha512>(data, key),
4653
}
4754
}

0 commit comments

Comments
 (0)