Skip to content

Commit

Permalink
capsule in feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nomick committed Jan 16, 2024
1 parent e30e19c commit f01f0c5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 4 additions & 1 deletion crates/s3s/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ categories = ["web-programming", "web-programming::http-server"]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
openssl = ["dep:openssl"]

[dependencies]
arrayvec = "0.7.4"
async-trait = "0.1.73"
Expand All @@ -33,7 +36,7 @@ memchr = "2.6.2"
mime = "0.3.17"
nom = "7.1.3"
nugine-rust-utils = "0.3.1"
openssl = { git = "https://telematik.prakinf.tu-ilmenau.de/gitlab/pub/rust-openssl.git", features = ["vendored"] }
openssl = { version = "0.10.62", optional = true }
pin-project-lite = "0.2.12"
quick-xml = { version = "0.31.0", features = ["serialize"] }
serde = { version = "1.0.183", features = ["derive"] }
Expand Down
37 changes: 32 additions & 5 deletions crates/s3s/src/sig_v4/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ use hex_simd::{AsOut, AsciiCase};
use hyper::body::Bytes;
use hyper::Method;
use rust_utils::str::StrExt;
use sha2::{Digest, Sha256};
use smallvec::SmallVec;
use zeroize::Zeroize;

#[cfg(not(feature = "openssl"))]
use sha2::{Digest, Sha256};

#[cfg(feature = "openssl")]
use openssl::hash::{Hasher, MessageDigest};

/// `f(hex(src))`
fn hex_bytes32<R>(src: &[u8; 32], f: impl FnOnce(&str) -> R) -> R {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 64];
Expand All @@ -25,21 +30,43 @@ fn hex_bytes32<R>(src: &[u8; 32], f: impl FnOnce(&str) -> R) -> R {
}

/// `f(hex(sha256(data)))`
#[cfg(not(feature = "openssl"))]
fn hex_sha256<R>(data: &[u8], f: impl FnOnce(&str) -> R) -> R {
let src = Sha256::digest(data);
hex_bytes32(src.as_ref(), f)
}

/// `f(hex(sha256(data)))`
#[cfg(feature = "openssl")]
fn hex_sha256<R>(data: &[u8], f: impl FnOnce(&str) -> R) -> R {
let src = {
let mut h = Hasher::new(MessageDigest::sha256()).unwrap();
h.update(data).unwrap();
h.finish().unwrap()
};
hex_bytes32(src.as_ref().try_into().unwrap(), f)
}

/// `f(hex(sha256(chunk)))`
#[cfg(not(feature = "openssl"))]
fn hex_sha256_chunk<R>(chunk: &[Bytes], f: impl FnOnce(&str) -> R) -> R {
let src = {
let mut h = openssl::hash::Hasher::new(openssl::hash::MessageDigest::sha256()).unwrap();
let mut h = Sha256::new();
chunk.iter().for_each(|data| h.update(data));
h.finalize()
};
hex_bytes32(src.as_ref(), f)
}

/// `f(hex(sha256(chunk)))`
#[cfg(feature = "openssl")]
fn hex_sha256_chunk<R>(chunk: &[Bytes], f: impl FnOnce(&str) -> R) -> R {
let src = {
let mut h = Hasher::new(MessageDigest::sha256()).unwrap();
chunk.iter().for_each(|data| h.update(data).unwrap());
h.finish().unwrap()
};
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 64];
let ans = hex_simd::encode_as_str(src.as_ref(), buf.as_out(), AsciiCase::Lower);
f(ans)
hex_bytes32(src.as_ref().try_into().unwrap(), f)
}

fn hex(data: impl AsRef<[u8]>) -> String {
Expand Down

0 comments on commit f01f0c5

Please sign in to comment.