thin-fetch 0.1.0

Low-level Git crate to fetch objects with high granularity
Documentation
use ring::digest::{Context, Digest};

use crate::HashKind;

#[must_use]
pub fn get_hash(typ: &str, data: &[u8], hash_kind: HashKind) -> Digest {
    let digest = match hash_kind {
        HashKind::Sha1 => &ring::digest::SHA1_FOR_LEGACY_USE_ONLY,
        HashKind::Sha256 => &ring::digest::SHA256,
    };

    let mut ctx = Context::new(digest);
    let hdr = format!("{typ} {}", data.len());
    ctx.update(hdr.as_bytes());
    ctx.update(b"\x00");
    ctx.update(data);
    ctx.finish()
}

#[must_use]
pub fn as_hex(data: &[u8]) -> String {
    use std::fmt::Write as _;

    data.iter().fold(String::new(), |mut acc, cur| {
        write!(acc, "{cur:02x}").unwrap();
        acc
    })
}