10_aea_roundtrip/
10_aea_roundtrip.rs1mod common;
2
3use common::{artifact_dir, path_string, pseudo_random_bytes};
4use compression::{
5 AeaContext, AeaContextField, AeaContextFieldRepresentation, AeaPadding, AeaProfile,
6 ArchiveCompressionAlgorithm, ArchiveFlags, ByteStream, DEFAULT_FILE_MODE, OPEN_CREATE,
7 OPEN_READ_ONLY, OPEN_READ_WRITE, OPEN_TRUNCATE,
8};
9
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11 let artifact_dir = artifact_dir("aea-roundtrip");
12 let archive_path = path_string(&artifact_dir.join("example.aea"));
13 let payload = pseudo_random_bytes(16 * 1024);
14
15 let mut context = AeaContext::with_profile(AeaProfile::HkdfSha256AesctrHmacSymmetricNone)?;
16 context.set_padding_size(AeaPadding::NONE)?;
17 context.set_compression_algorithm(ArchiveCompressionAlgorithm::Lzfse)?;
18 context.generate_field_blob(AeaContextField::SymmetricKey)?;
19
20 let stream = ByteStream::open_with_path(
21 &archive_path,
22 OPEN_READ_WRITE | OPEN_CREATE | OPEN_TRUNCATE,
23 DEFAULT_FILE_MODE,
24 )?;
25 let mut encrypted = context.encryption_output_stream(stream, ArchiveFlags::empty(), 0)?;
26 encrypted.write_all(&payload)?;
27 context.close_encryption_output_stream(&mut encrypted)?;
28
29 let symmetric_key = context.field_blob(
30 AeaContextField::SymmetricKey,
31 AeaContextFieldRepresentation::Raw,
32 )?;
33 let mut input = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34 let mut decrypt_context = AeaContext::from_encrypted_stream(&mut input)?;
35 decrypt_context.set_symmetric_key(&symmetric_key)?;
36 let mut decrypted = decrypt_context.decryption_input_stream(input, ArchiveFlags::empty(), 0)?;
37 assert_eq!(decrypted.read_to_end()?, payload);
38
39 println!(
40 "raw_size={} container_size={}",
41 context.raw_size()?,
42 context.container_size()?
43 );
44 println!("✅ AppleEncryptedArchive roundtrip OK");
45 Ok(())
46}