mirror of
https://github.com/jmic/altcha-lib-rs.git
synced 2026-01-25 04:16:40 +00:00
moved example, ensure compatibility to stable dependencies, improved Cargo.toml
This commit is contained in:
2
.github/workflows/build-and-test.yml
vendored
2
.github/workflows/build-and-test.yml
vendored
@@ -30,7 +30,7 @@ jobs:
|
||||
- windows-latest
|
||||
runs-on: ${{ matrix.os }}
|
||||
env:
|
||||
FEATURES: ${{ matrix.rust != 'nightly' && matrix.features || format('{0},nightly', matrix.features) }}
|
||||
FEATURES: ${{ matrix.features }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup ${{ matrix.rust }} Rust toolchain with caching
|
||||
|
||||
36
Cargo.toml
36
Cargo.toml
@@ -2,18 +2,32 @@
|
||||
name = "altcha-lib-rs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = ["jmic <jmic@users.noreply.github.com>"]
|
||||
description = "Community implementation of the Altcha library in Rust for your own server applications to create and validate challenges and responses."
|
||||
license = "Apache-2.0"
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[example]]
|
||||
name = "server"
|
||||
path = "examples/server.rs"
|
||||
required-features = ["json"]
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.38"
|
||||
rand = "0.9.0-alpha.2"
|
||||
sha2 = "0.11.0-pre.4"
|
||||
base16ct = { version = "0.2.0", features = ["alloc"] }
|
||||
sha1 = "0.11.0-pre.4"
|
||||
hmac = "0.13.0-pre.4"
|
||||
serde = { version = "1.0.208", features = ["derive"] }
|
||||
serde_json = { version = "1.0.125", optional = true }
|
||||
|
||||
chrono = "0.4"
|
||||
rand = "0"
|
||||
sha2 = "0"
|
||||
base16ct = { version = "0.2", features = ["alloc"] }
|
||||
sha1 = "0"
|
||||
hmac = "0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = { version = "1.0", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["json"]
|
||||
json = ["serde_json"]
|
||||
default = []
|
||||
json = ["serde_json"]
|
||||
|
||||
[dev-dependencies]
|
||||
actix-web = "4"
|
||||
base64 = "0.22"
|
||||
@@ -1,15 +0,0 @@
|
||||
[package]
|
||||
name = "server"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
serde = { version = "1.0.208", features = ["derive"] }
|
||||
chrono = "0.4.38"
|
||||
base64 = "0.22.1"
|
||||
|
||||
[dependencies.altcha-lib-rs]
|
||||
default-features = true
|
||||
path = "../.."
|
||||
@@ -1,5 +1,6 @@
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
#[cfg(feature = "json")]
|
||||
ParseJson(serde_json::Error),
|
||||
ParseInteger(std::num::ParseIntError),
|
||||
ParseExpire(String),
|
||||
@@ -11,6 +12,7 @@ pub enum Error {
|
||||
General(String)
|
||||
}
|
||||
|
||||
#[cfg(feature = "json")]
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(other: serde_json::Error) -> Self {
|
||||
Self::ParseJson(other)
|
||||
|
||||
@@ -242,7 +242,7 @@ pub fn verify_solution(payload: &Payload, hmac_key: &str, check_expire: bool) ->
|
||||
/// hmac_key: "super-secret",
|
||||
/// expires: Some(Utc::now()+chrono::TimeDelta::minutes(1)),
|
||||
/// ..Default::default()
|
||||
/// })?;
|
||||
/// }).expect("internal error");
|
||||
/// let res = solve_challenge(&challenge.challenge, &challenge.salt,
|
||||
/// Some(challenge.algorithm), Some(challenge.maxnumber), 0);
|
||||
/// ```
|
||||
|
||||
10
src/utils.rs
10
src/utils.rs
@@ -2,7 +2,7 @@ use rand::Rng;
|
||||
use sha1::Sha1;
|
||||
use sha2::{Sha256, Sha512};
|
||||
use hmac::digest::Digest;
|
||||
use hmac::{Hmac, KeyInit, Mac};
|
||||
use hmac::{Hmac, Mac};
|
||||
use std::collections::HashMap;
|
||||
use crate::algorithm::AltchaAlgorithm;
|
||||
|
||||
@@ -20,7 +20,7 @@ pub fn random_bytes(len: usize) -> Vec<u8> {
|
||||
|
||||
pub fn random_int(max: u64) -> u64 {
|
||||
let mut rng = rand::thread_rng();
|
||||
let dist = rand::distr::Uniform::new_inclusive(0, max).unwrap();
|
||||
let dist = rand::distributions::Uniform::new_inclusive(0, max);
|
||||
rng.sample(&dist)
|
||||
}
|
||||
|
||||
@@ -47,19 +47,19 @@ pub fn hmac_function(altcha_algorithm: &AltchaAlgorithm, data: &str, key: &str)
|
||||
let mut mac = HmacSha1::new_from_slice(key.as_bytes()).expect("HMAC can take key of any size");
|
||||
mac.update(data.as_bytes());
|
||||
let res = mac.finalize();
|
||||
base16ct::lower::encode_string(res.as_bytes())
|
||||
base16ct::lower::encode_string(&res.into_bytes())
|
||||
}
|
||||
AltchaAlgorithm::Sha256 => {
|
||||
let mut mac = HmacSha256::new_from_slice(key.as_bytes()).expect("HMAC can take key of any size");
|
||||
mac.update(data.as_bytes());
|
||||
let res = mac.finalize();
|
||||
base16ct::lower::encode_string(res.as_bytes())
|
||||
base16ct::lower::encode_string(&res.into_bytes())
|
||||
}
|
||||
AltchaAlgorithm::Sha512 => {
|
||||
let mut mac = HmacSha512::new_from_slice(key.as_bytes()).expect("HMAC can take key of any size");
|
||||
mac.update(data.as_bytes());
|
||||
let res = mac.finalize();
|
||||
base16ct::lower::encode_string(res.as_bytes())
|
||||
base16ct::lower::encode_string(&res.into_bytes())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user