Skip to content

Commit 0cf3d14

Browse files
mickvandijkeclaude
andcommitted
fix: compile errors in tests, thread child_level through DecryptionStream, fix stale docstring
- Fix invalid `xor_name::crate::hash::content_hash` syntax in src/tests.rs (write_and_read and seek_over_chunk_limit) — should be `crate::hash::` - Thread child_level through DecryptionStream instead of hardcoding 0, so streaming decrypt uses the correct KDF domain separation - Fix Python docstring that still said "SHA256" instead of "BLAKE3" Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5010d47 commit 0cf3d14

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

src/python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl PyEncryptedChunk {
246246
/// Get the hash of the encrypted chunk.
247247
///
248248
/// Returns:
249-
/// bytes: The SHA256 hash of the encrypted chunk.
249+
/// bytes: The BLAKE3 hash of the encrypted chunk.
250250
pub fn hash(&self) -> Cow<'_, [u8]> {
251251
crate::hash::content_hash(&self.inner.content)
252252
.0

src/stream_decrypt.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use xor_name::XorName;
2828
pub struct DecryptionStream<F> {
2929
chunk_infos: Vec<ChunkInfo>,
3030
src_hashes: Vec<XorName>,
31+
child_level: usize,
3132
get_chunk_parallel: F,
3233
current_batch_start: usize,
3334
current_batch_chunks: Vec<Bytes>,
@@ -51,13 +52,15 @@ where
5152
data_map.clone()
5253
};
5354

55+
let child_level = root_map.child().unwrap_or(0);
5456
let mut chunk_infos = root_map.infos().to_vec();
5557
chunk_infos.sort_by_key(|info| info.index);
5658
let src_hashes = extract_hashes(&root_map);
5759

5860
Ok(Self {
5961
chunk_infos,
6062
src_hashes,
63+
child_level,
6164
get_chunk_parallel,
6265
current_batch_start: 0,
6366
current_batch_chunks: Vec::new(),
@@ -100,8 +103,12 @@ where
100103
for (info, (_index, encrypted_content)) in
101104
batch_infos.iter().zip(fetched_chunks.into_iter())
102105
{
103-
let decrypted_chunk =
104-
decrypt_chunk(info.index, &encrypted_content, &self.src_hashes, 0)?;
106+
let decrypted_chunk = decrypt_chunk(
107+
info.index,
108+
&encrypted_content,
109+
&self.src_hashes,
110+
self.child_level,
111+
)?;
105112
self.current_batch_chunks.push(decrypted_chunk);
106113
}
107114

@@ -213,7 +220,12 @@ where
213220
let mut all_bytes = Vec::new();
214221
for chunk_index in start_chunk..=end_chunk {
215222
if let Some(encrypted_content) = chunk_map.get(&chunk_index) {
216-
let decrypted = decrypt_chunk(chunk_index, encrypted_content, &self.src_hashes, 0)?;
223+
let decrypted = decrypt_chunk(
224+
chunk_index,
225+
encrypted_content,
226+
&self.src_hashes,
227+
self.child_level,
228+
)?;
217229
all_bytes.extend_from_slice(&decrypted);
218230
}
219231
}
@@ -962,6 +974,7 @@ mod tests {
962974
let mock_stream = DecryptionStream {
963975
chunk_infos: data_map.infos().to_vec(),
964976
src_hashes: vec![crate::hash::content_hash(&[0u8]); num_chunks], // Dummy hashes
977+
child_level: 0,
965978
get_chunk_parallel,
966979
current_batch_start: 0,
967980
current_batch_chunks: Vec::new(),

src/tests.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// permissions and limitations relating to use of the SAFE Network Software.
88

99
use crate::{
10-
decrypt_full_set, decrypt_range, encrypt, get_chunk_size, get_num_chunks,
10+
decrypt_full_set, decrypt_range, encrypt, get_chunk_size, get_num_chunks,
1111
test_helpers::random_bytes, DataMap, EncryptedChunk, Error, StreamSelfDecryptor,
1212
StreamSelfEncryptor, MIN_ENCRYPTABLE_BYTES,
1313
};
@@ -20,17 +20,17 @@ fn test_stream_self_encryptor() {
2020
// Create a temporary directory for our test files
2121
let dir = tempdir().unwrap();
2222
println!("Created temp dir at: {:?}", dir.path());
23-
23+
2424
// Create input file path and write test data
2525
let file_path = dir.path().join("input_file");
2626
let file_size = 10 * 1024 * 1024; // 10MB
2727
let data = random_bytes(file_size);
28-
28+
2929
// Create parent directory if it doesn't exist
3030
if let Some(parent) = file_path.parent() {
3131
std::fs::create_dir_all(parent).unwrap();
3232
}
33-
33+
3434
std::fs::write(&file_path, &data).unwrap();
3535
println!("Written test data to: {:?}", file_path);
3636

@@ -44,14 +44,12 @@ fn test_stream_self_encryptor() {
4444
assert!(file_path.exists(), "Input file does not exist");
4545

4646
// Encrypt the file using StreamSelfEncryptor
47-
let mut encryptor = StreamSelfEncryptor::encrypt_from_file(
48-
file_path,
49-
Some(chunk_path)
50-
).unwrap();
51-
47+
let mut encryptor =
48+
StreamSelfEncryptor::encrypt_from_file(file_path, Some(chunk_path)).unwrap();
49+
5250
let mut encrypted_chunks = Vec::new();
5351
let mut data_map = None;
54-
52+
5553
while let Ok((chunk, map)) = encryptor.next_encryption() {
5654
if let Some(c) = chunk {
5755
encrypted_chunks.push(c);
@@ -61,22 +59,20 @@ fn test_stream_self_encryptor() {
6159
break;
6260
}
6361
}
64-
62+
6563
let data_map = data_map.expect("Encryption should produce a data map");
6664

6765
// Create output file path for decryption
6866
let decrypted_file_path = dir.path().join("decrypted_file");
69-
67+
7068
// Create parent directory for output file if it doesn't exist
7169
if let Some(parent) = decrypted_file_path.parent() {
7270
std::fs::create_dir_all(parent).unwrap();
7371
}
7472

7573
// Initialize decryptor
76-
let mut decryptor = StreamSelfDecryptor::decrypt_to_file(
77-
decrypted_file_path.clone(),
78-
&data_map
79-
).unwrap();
74+
let mut decryptor =
75+
StreamSelfDecryptor::decrypt_to_file(decrypted_file_path.clone(), &data_map).unwrap();
8076

8177
// Process all chunks
8278
for chunk in encrypted_chunks {
@@ -88,7 +84,11 @@ fn test_stream_self_encryptor() {
8884

8985
// Verify the decrypted content matches original
9086
let decrypted_data = std::fs::read(&decrypted_file_path).unwrap();
91-
assert_eq!(data.to_vec(), decrypted_data, "Decrypted data should match original");
87+
assert_eq!(
88+
data.to_vec(),
89+
decrypted_data,
90+
"Decrypted data should match original"
91+
);
9292
}
9393

9494
#[test]
@@ -102,7 +102,7 @@ fn write_and_read() -> Result<(), Error> {
102102

103103
let chunk_hashes: Vec<_> = encrypted_chunks
104104
.iter()
105-
.map(|chunk| xor_name::crate::hash::content_hash(&chunk.content))
105+
.map(|chunk| crate::hash::content_hash(&chunk.content))
106106
.collect();
107107
dbg!(&chunk_hashes);
108108

@@ -236,7 +236,7 @@ fn seek_over_chunk_limit() -> Result<(), Error> {
236236
get_root_data_map(data_map.clone(), &mut |hash| {
237237
encrypted_chunks
238238
.iter()
239-
.find(|chunk| xor_name::crate::hash::content_hash(&chunk.content) == hash)
239+
.find(|chunk| crate::hash::content_hash(&chunk.content) == hash)
240240
.map(|chunk| chunk.content.clone())
241241
.ok_or_else(|| Error::Generic(format!("Chunk not found for hash: {:?}", hash)))
242242
})?

0 commit comments

Comments
 (0)