Skip to content

Commit 2d1e5d5

Browse files
nomickNugine
andauthored
Added Openssl feature (#125)
* try to use sha from openssl * capsule in feature * disable openssl on windows * s3s: utils: crypto: openssl sha256 --------- Co-authored-by: Nugine <[email protected]>
1 parent cff9779 commit 2d1e5d5

File tree

3 files changed

+63
-32
lines changed

3 files changed

+63
-32
lines changed

crates/s3s/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ categories = ["web-programming", "web-programming::http-server"]
1313
all-features = true
1414
rustdoc-args = ["--cfg", "docsrs"]
1515

16+
[features]
17+
openssl = ["dep:openssl"]
18+
19+
[target.'cfg(not(windows))'.dependencies]
20+
openssl = { version = "0.10.62", optional = true }
21+
1622
[dependencies]
1723
arrayvec = "0.7.4"
1824
async-trait = "0.1.73"

crates/s3s/src/sig_v4/methods.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,15 @@ use super::AmzDate;
44

55
use crate::auth::SecretKey;
66
use crate::http::OrderedHeaders;
7-
use crate::utils::crypto::hmac_sha256;
7+
use crate::utils::crypto::{hex, hex_sha256, hex_sha256_chunk, hmac_sha256};
88
use crate::utils::stable_sort_by_first;
99

10-
use std::mem::MaybeUninit;
11-
12-
use hex_simd::{AsOut, AsciiCase};
1310
use hyper::body::Bytes;
1411
use hyper::Method;
1512
use rust_utils::str::StrExt;
16-
use sha2::{Digest, Sha256};
1713
use smallvec::SmallVec;
1814
use zeroize::Zeroize;
1915

20-
/// `f(hex(src))`
21-
fn hex_bytes32<R>(src: &[u8; 32], f: impl FnOnce(&str) -> R) -> R {
22-
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 64];
23-
let ans = hex_simd::encode_as_str(src.as_ref(), buf.as_out(), AsciiCase::Lower);
24-
f(ans)
25-
}
26-
27-
/// `f(hex(sha256(data)))`
28-
fn hex_sha256<R>(data: &[u8], f: impl FnOnce(&str) -> R) -> R {
29-
let src = Sha256::digest(data);
30-
hex_bytes32(src.as_ref(), f)
31-
}
32-
33-
/// `f(hex(sha256(chunk)))`
34-
fn hex_sha256_chunk<R>(chunk: &[Bytes], f: impl FnOnce(&str) -> R) -> R {
35-
let src = {
36-
let mut h = Sha256::new();
37-
chunk.iter().for_each(|data| h.update(data));
38-
h.finalize()
39-
};
40-
hex_bytes32(src.as_ref(), f)
41-
}
42-
43-
fn hex(data: impl AsRef<[u8]>) -> String {
44-
hex_simd::encode_to_string(data, hex_simd::AsciiCase::Lower)
45-
}
46-
4716
/// custom uri encode
4817
#[allow(clippy::indexing_slicing, clippy::inline_always, clippy::unwrap_used)]
4918
fn uri_encode(output: &mut String, input: &str, encode_slash: bool) {

crates/s3s/src/utils/crypto.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
use std::mem::MaybeUninit;
2+
3+
use hex_simd::{AsOut, AsciiCase};
4+
use hyper::body::Bytes;
5+
16
/// verify sha256 checksum string
27
pub fn is_sha256_checksum(s: &str) -> bool {
38
// TODO: optimize
@@ -24,3 +29,54 @@ pub fn hmac_sha256(key: impl AsRef<[u8]>, data: impl AsRef<[u8]>) -> [u8; 32] {
2429
m.update(data.as_ref());
2530
m.finalize().into_bytes().into()
2631
}
32+
33+
pub fn hex(data: impl AsRef<[u8]>) -> String {
34+
hex_simd::encode_to_string(data, hex_simd::AsciiCase::Lower)
35+
}
36+
37+
/// `f(hex(src))`
38+
fn hex_bytes32<R>(src: impl AsRef<[u8]>, f: impl FnOnce(&str) -> R) -> R {
39+
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 64];
40+
let ans = hex_simd::encode_as_str(src.as_ref(), buf.as_out(), AsciiCase::Lower);
41+
f(ans)
42+
}
43+
44+
#[cfg(not(all(feature = "openssl", not(windows))))]
45+
fn sha256(data: &[u8]) -> impl AsRef<[u8; 32]> {
46+
use sha2::{Digest, Sha256};
47+
<Sha256 as Digest>::digest(data)
48+
}
49+
50+
#[cfg(all(feature = "openssl", not(windows)))]
51+
fn sha256(data: &[u8]) -> impl AsRef<[u8]> {
52+
use openssl::hash::{Hasher, MessageDigest};
53+
let mut h = Hasher::new(MessageDigest::sha256()).unwrap();
54+
h.update(data).unwrap();
55+
h.finish().unwrap()
56+
}
57+
58+
#[cfg(not(all(feature = "openssl", not(windows))))]
59+
fn sha256_chunk(chunk: &[Bytes]) -> impl AsRef<[u8; 32]> {
60+
use sha2::{Digest, Sha256};
61+
let mut h = <Sha256 as Digest>::new();
62+
chunk.iter().for_each(|data| h.update(data));
63+
h.finalize()
64+
}
65+
66+
#[cfg(all(feature = "openssl", not(windows)))]
67+
fn sha256_chunk(chunk: &[Bytes]) -> impl AsRef<[u8]> {
68+
use openssl::hash::{Hasher, MessageDigest};
69+
let mut h = Hasher::new(MessageDigest::sha256()).unwrap();
70+
chunk.iter().for_each(|data| h.update(data).unwrap());
71+
h.finish().unwrap()
72+
}
73+
74+
/// `f(hex(sha256(data)))`
75+
pub fn hex_sha256<R>(data: &[u8], f: impl FnOnce(&str) -> R) -> R {
76+
hex_bytes32(sha256(data).as_ref(), f)
77+
}
78+
79+
/// `f(hex(sha256(chunk)))`
80+
pub fn hex_sha256_chunk<R>(chunk: &[Bytes], f: impl FnOnce(&str) -> R) -> R {
81+
hex_bytes32(sha256_chunk(chunk).as_ref(), f)
82+
}

0 commit comments

Comments
 (0)