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

Function encrypt_segment_bytes

nodedb/src/storage/segment.rs:191–215  ·  view source on GitHub ↗

Encrypt segment data into a self-describing byte envelope (no file I/O). Equivalent to `write_encrypted_segment` but returns the full envelope bytes instead of writing to disk. Used by object-store upload paths where the caller manages persistence.

(
    data: &[u8],
    footer: &SegmentFooter,
    key: Option<&nodedb_wal::crypto::WalEncryptionKey>,
)

Source from the content-addressed store, hash-verified

189/// bytes instead of writing to disk. Used by object-store upload paths where
190/// the caller manages persistence.
191pub fn encrypt_segment_bytes(
192 data: &[u8],
193 footer: &SegmentFooter,
194 key: Option<&nodedb_wal::crypto::WalEncryptionKey>,
195) -> crate::Result<Vec<u8>> {
196 let mut out = Vec::new();
197 if let Some(key) = key {
198 let fresh_key = key.with_fresh_epoch().map_err(crate::Error::Wal)?;
199 let epoch = *fresh_key.epoch();
200 let preamble = SegmentPreamble::new_seg(epoch);
201 let preamble_bytes = preamble.to_bytes();
202 let ciphertext = fresh_key
203 .encrypt_aad(footer.min_lsn.as_u64(), &preamble_bytes, data)
204 .map_err(|e| crate::Error::Storage {
205 engine: "segment".into(),
206 detail: format!("segment encryption failed: {e}"),
207 })?;
208 out.extend_from_slice(&preamble_bytes);
209 out.extend_from_slice(&ciphertext);
210 } else {
211 out.extend_from_slice(data);
212 }
213 out.extend_from_slice(&footer.to_bytes());
214 Ok(out)
215}
216
217/// Decrypt a segment byte envelope (no file I/O).
218///

Callers 1

create_base_snapshotFunction · 0.85

Calls 5

with_fresh_epochMethod · 0.80
encrypt_aadMethod · 0.80
epochMethod · 0.45
to_bytesMethod · 0.45
as_u64Method · 0.45

Tested by

no test coverage detected