Skip to main content

loonfs_api/
digest.rs

1//! The `sha256:<64hex>` digest form durable formats use.
2
3use sha2::{Digest, Sha256};
4use std::fmt::Write as _;
5
6/// Computes the durable `sha256:` digest spelling used by envelope payloads
7/// and local compare tokens.
8pub fn sha256_digest(bytes: &[u8]) -> String {
9    format!("sha256:{}", sha256_hex(bytes))
10}
11
12pub(crate) fn sha256_hex(bytes: &[u8]) -> String {
13    let digest = Sha256::digest(bytes);
14    let mut encoded = String::with_capacity(digest.len() * 2);
15
16    for byte in digest {
17        write!(&mut encoded, "{byte:02x}").expect("writing to a String should not fail");
18    }
19
20    encoded
21}