use std::fs; use fcry::{ DecryptOptions, DecryptRangeOptions, EncryptOptions, KdfParams, OutputOptions, SecretBytes32, decrypt, decrypt_range, default_argon_decrypt_cap_mib, encrypt, }; use tempfile::TempDir; fn test_key() -> SecretBytes32 { let mut key = SecretBytes32::zeroed(); key.with_mut_array(|key| key.copy_from_slice(b"0123456789abcdef0123456789abcdef")); key } #[test] fn library_file_roundtrip_raw_key() { let dir = TempDir::new().unwrap(); let plain = dir.path().join("plain.bin"); let ct = dir.path().join("cipher.fcry"); let out = dir.path().join("out.bin"); let data: Vec = (0..=255).cycle().take(100_000).collect(); fs::write(&plain, &data).unwrap(); let key = test_key(); let encrypt_options = EncryptOptions { input_file: Some(plain), output_file: Some(ct.clone()), chunk_size: 4096, threads: 1, output: OutputOptions::default(), }; encrypt(&encrypt_options, &key, KdfParams::Raw).unwrap(); let decrypt_options = DecryptOptions { input_file: Some(ct), output_file: Some(out.clone()), threads: 1, ..DecryptOptions::default() }; decrypt(&decrypt_options, Some(&key), None).unwrap(); assert_eq!(fs::read(out).unwrap(), data); } #[test] fn library_range_decrypt_raw_key() { let dir = TempDir::new().unwrap(); let plain = dir.path().join("plain.bin"); let ct = dir.path().join("cipher.fcry"); let out = dir.path().join("slice.bin"); let data: Vec = (0..=255).cycle().take(50_000).collect(); fs::write(&plain, &data).unwrap(); let key = test_key(); let encrypt_options = EncryptOptions { input_file: Some(plain), output_file: Some(ct.clone()), chunk_size: 1024, threads: 2, output: OutputOptions::default(), }; encrypt(&encrypt_options, &key, KdfParams::Raw).unwrap(); let range_options = DecryptRangeOptions { input_file: ct, output_file: Some(out.clone()), offset: 1234, length: 20_000, max_argon_memory_mib: default_argon_decrypt_cap_mib(), output: OutputOptions::default(), }; decrypt_range(&range_options, Some(&key), None).unwrap(); assert_eq!(fs::read(out).unwrap(), data[1234..21_234]); }