MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / detect_encryption

Function detect_encryption

nodedb-array/src/segment/encrypt.rs:51–70  ·  view source on GitHub ↗

Sniff the first 4 bytes to determine if `blob` is a plaintext (`NDAS`) or encrypted (`SEGA`) segment. Returns `true` if encrypted. Returns an error if the magic is neither `NDAS` nor `SEGA`.

(blob: &[u8])

Source from the content-addressed store, hash-verified

49///
50/// Returns an error if the magic is neither `NDAS` nor `SEGA`.
51pub(crate) fn detect_encryption(blob: &[u8]) -> Result<bool, ArrayError> {
52 if blob.len() < 4 {
53 return Err(ArrayError::SegmentCorruption {
54 detail: format!("segment too short to sniff magic: {} bytes", blob.len()),
55 });
56 }
57 let magic = &blob[..4];
58 if magic == NDAS_MAGIC {
59 Ok(false)
60 } else if magic == SEGA_MAGIC {
61 Ok(true)
62 } else {
63 Err(ArrayError::SegmentCorruption {
64 detail: format!(
65 "unrecognised segment magic: {magic:02x?} \
66 (expected NDAS or SEGA)"
67 ),
68 })
69 }
70}
71
72#[cfg(test)]
73mod tests {

Callers 1

open_with_kekMethod · 0.85

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected