http-content-digest

Crates.iohttp-content-digest
lib.rshttp-content-digest
version0.1.2
created_at2025-06-03 22:11:52.408028+00
updated_at2025-08-22 07:25:07.348032+00
description`Content-Digest` implementation to Request/Response of `http` crate.
homepage
repositoryhttps://github.com/ShuttlePub/http-msgsign
max_upload_size
id1699560
size30,661
RechellaTek (HalsekiRaika)

documentation

README

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;

// Trait representing the algorithm for taking the Digest of a Body.
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())
    }
}

// {"hello": "world"}
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 = create_request();    
    
    // Digest of `Request<B>` can be done with this, 
    // but note that after execution it becomes `Request<BoxBody<Bytes, DigestError>>`.
    // This is because Content-Digest is expected to be 
    // the last processing of the request or response.
    let request: Request<BoxBody<Bytes, DigestError>> = request.digest::<Sha256Hasher>().await.unwrap();
    
    assert!(request.headers().contains_key("content-digest"));
    
    let digest = request.headers().get("content-digest").unwrap().to_str().unwrap();
    
    assert_eq!(digest, "sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:");
}
Commit count: 44

cargo fmt