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>,
)
| 189 | /// bytes instead of writing to disk. Used by object-store upload paths where |
| 190 | /// the caller manages persistence. |
| 191 | pub 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 | /// |
no test coverage detected