http-content-digest
This library enables a process,
Content-Digest of RFC 9530 - Digest Fields
to Request/Response in http crate.
Usage
pub struct Sha256Hasher;
impl ContentHasher for Sha256Hasher {
const DIGEST_ALG: &'static str = "sha-256";
fn hash(content: &[u8]) -> DigestHash {
let mut hasher = <sha2::Sha256 as Digest>::new();
hasher.update(content);
DigestHash::new(hasher.finalize().to_vec())
}
}
pub fn create_body() -> Full<Bytes> {
Full::new(Bytes::from_static(b"{\"hello\": \"world\"}"))
}
pub fn create_request() -> Request<BoxBody<Bytes, Infallible>> {
Request::builder()
.method("GET")
.uri("https://example.com/")
.header("date", "Tue, 07 Jun 2014 20:51:35 GMT")
.header("content-type", "application/json")
.body(BoxBody::new(create_body()))
.unwrap()
}
#[tokio::main]
async fn main() {
let request: Request<BoxBody<Bytes, DigestError>> = request.digest::<Sha256Hasher>().await.unwrap();
assert!(req.headers().contains_key("content-digest"));
let digest = req.headers().get("content-digest").unwrap().to_str().unwrap();
assert_eq!(digest, "sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:");
}