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 content and envelope references.
7pub fn sha256_digest(bytes: &[u8]) -> String {
8 format!("sha256:{}", sha256_hex(bytes))
9}
10
11pub(crate) fn sha256_hex(bytes: &[u8]) -> String {
12 let digest = Sha256::digest(bytes);
13 let mut encoded = String::with_capacity(digest.len() * 2);
14
15 for byte in digest {
16 write!(&mut encoded, "{byte:02x}").expect("writing to a String should not fail");
17 }
18
19 encoded
20}