Skip to main content

qualia_core_db/bundle/stream/descriptor/
digest.rs

1//! Domain framing only; the canonical crypto library owns SHA384.
2use super::{DescriptorError, MAX_ENCODED};
3use crate::crypto::network::digest::sha384;
4use crate::net::qdnf::types::StrongDigest;
5pub const HASH_SCRATCH_BYTES: usize = MAX_ENCODED + 64;
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum DigestDomain {
8    Context,
9    ContextPage,
10    EntryPage,
11    Body,
12}
13pub fn domain_digest(
14    domain: DigestDomain,
15    encoded: &[u8],
16    scratch: &mut [u8],
17) -> Result<StrongDigest, DescriptorError> {
18    if encoded.len() > MAX_ENCODED {
19        return Err(DescriptorError::Limit);
20    }
21    let prefix: &[u8] = match domain {
22        DigestDomain::Context => b"QBDL-QASD-CONTEXT-V1\0",
23        DigestDomain::ContextPage => b"QBDL-QASD-CONTEXT-PAGE-V1\0",
24        DigestDomain::EntryPage => b"QBDL-QASD-ENTRY-PAGE-V1\0",
25        DigestDomain::Body => b"QBDL-QASD-BODY-V1\0",
26    };
27    let len = prefix
28        .len()
29        .checked_add(encoded.len())
30        .ok_or(DescriptorError::Overflow)?;
31    let out = scratch.get_mut(..len).ok_or(DescriptorError::Capacity)?;
32    out[..prefix.len()].copy_from_slice(prefix);
33    out[prefix.len()..].copy_from_slice(encoded);
34    Ok(sha384(out))
35}