Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions examples/basic_encryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct Args {
}

fn to_hex(ch: u8) -> String {
fmt::format(format_args!("{:02x}", ch))
fmt::format(format_args!("{ch:02x}"))
}

fn file_name(name: XorName) -> String {
Expand Down Expand Up @@ -115,7 +115,7 @@ impl DiskBasedStorage {
let mut file = File::create(&path)?;
file.write_all(&data[..])
.map(|_| {
println!("Chunk written to {:?}", path);
println!("Chunk written to {path:?}");
})
.map_err(From::from)
}
Expand All @@ -128,7 +128,7 @@ async fn main() {
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
if args.flag_help {
println!("{:?}", args)
println!("{args:?}")
}

let mut chunk_store_dir = env::temp_dir();
Expand All @@ -145,7 +145,7 @@ async fn main() {
let mut data = Vec::new();
match file.read_to_end(&mut data) {
Ok(_) => (),
Err(error) => return println!("{}", error),
Err(error) => return println!("{error}"),
}

let (data_map, encrypted_chunks) = encrypt(Bytes::from(data)).unwrap();
Expand All @@ -163,20 +163,14 @@ async fn main() {
Ok(mut file) => {
let encoded = serialize(&data_map).unwrap();
match file.write_all(&encoded[..]) {
Ok(_) => println!("Data map written to {:?}", data_map_file),
Ok(_) => println!("Data map written to {data_map_file:?}"),
Err(error) => {
println!(
"Failed to write data map to {:?} - {:?}",
data_map_file, error
);
println!("Failed to write data map to {data_map_file:?} - {error:?}");
}
}
}
Err(error) => {
println!(
"Failed to create data map at {:?} - {:?}",
data_map_file, error
);
println!("Failed to create data map at {data_map_file:?} - {error:?}");
}
}
} else {
Expand Down Expand Up @@ -214,7 +208,7 @@ async fn main() {
let content =
decrypt(&DataMap::new(keys), encrypted_chunks.as_ref()).unwrap();
match file.write_all(&content[..]) {
Err(error) => println!("File write failed - {:?}", error),
Err(error) => println!("File write failed - {error:?}"),
Ok(_) => {
println!("File decrypted to {:?}", args.arg_destination.unwrap())
}
Expand All @@ -228,7 +222,7 @@ async fn main() {
}
}
} else {
println!("Failed to open data map at {:?}", data_map_file);
println!("Failed to open data map at {data_map_file:?}");
}
}
}
31 changes: 17 additions & 14 deletions examples/parallel_streaming_decryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ fn validate_paths(args: &Args) -> Result<()> {
// Check data map file exists and is readable
if !Path::new(&args.data_map).exists() {
return Err(Error::Generic(format!(
"Data map file does not exist: {}",
args.data_map
"Data map file does not exist: {file}",
file = args.data_map
)));
}

// Check chunks directory exists and is readable
let chunks_dir = Path::new(&args.chunks_dir);
if !chunks_dir.exists() {
return Err(Error::Generic(format!(
"Chunks directory does not exist: {}",
args.chunks_dir
"Chunks directory does not exist: {dir}",
dir = args.chunks_dir
)));
}
if !chunks_dir.is_dir() {
return Err(Error::Generic(format!(
"Chunks path is not a directory: {}",
args.chunks_dir
"Chunks path is not a directory: {dir}",
dir = args.chunks_dir
)));
}

Expand All @@ -51,8 +51,8 @@ fn validate_paths(args: &Args) -> Result<()> {
if let Some(parent) = output_path.parent() {
if !parent.exists() {
return Err(Error::Generic(format!(
"Output directory does not exist: {}",
parent.display()
"Output directory does not exist: {dir}",
dir = parent.display()
)));
}
// Try to verify write permissions
Expand All @@ -62,8 +62,8 @@ fn validate_paths(args: &Args) -> Result<()> {
.unwrap_or(true)
{
return Err(Error::Generic(format!(
"Output directory is not writable: {}",
parent.display()
"Output directory is not writable: {dir}",
dir = parent.display()
)));
}
}
Expand All @@ -89,7 +89,7 @@ fn main() -> Result<()> {
let mut chunk_data = Vec::new();
File::open(&chunk_path)
.and_then(|mut file| file.read_to_end(&mut chunk_data))
.map_err(|e| Error::Generic(format!("Failed to read chunk: {}", e)))?;
.map_err(|e| Error::Generic(format!("Failed to read chunk: {e}")))?;
Ok(Bytes::from(chunk_data))
})
.collect()
Expand All @@ -98,17 +98,20 @@ fn main() -> Result<()> {
// Use the streaming decryption function
streaming_decrypt_from_storage(&data_map, Path::new(&args.output), get_chunk_parallel)?;

println!("Successfully decrypted file to: {}", args.output);
println!(
"Successfully decrypted file to: {output}",
output = args.output
);

Ok(())
}

// Helper function to load data map from a file
fn load_data_map(path: &str) -> Result<DataMap> {
let mut file =
File::open(path).map_err(|e| Error::Generic(format!("Failed to open data map: {}", e)))?;
File::open(path).map_err(|e| Error::Generic(format!("Failed to open data map: {e}")))?;
let mut data = Vec::new();
file.read_to_end(&mut data)
.map_err(|e| Error::Generic(format!("Failed to read data map: {}", e)))?;
.map_err(|e| Error::Generic(format!("Failed to read data map: {e}")))?;
deserialize(&data)
}
2 changes: 1 addition & 1 deletion src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use bytes::Bytes;

/// The actual encrypted content of the chunk
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EncryptedChunk {
/// The encrypted content of the chunk
pub content: Bytes,
Expand Down
8 changes: 4 additions & 4 deletions src/data_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ impl Debug for DataMap {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
writeln!(formatter, "DataMap:")?;
if let Some(child) = self.child {
writeln!(formatter, " child: {}", child)?;
writeln!(formatter, " child: {child}")?;
}
let len = self.chunk_identifiers.len();
for (index, chunk) in self.chunk_identifiers.iter().enumerate() {
if index + 1 == len {
write!(formatter, " {:?}", chunk)?
write!(formatter, " {chunk:?}")?
} else {
writeln!(formatter, " {:?}", chunk)?
writeln!(formatter, " {chunk:?}")?
}
}
Ok(())
Expand Down Expand Up @@ -124,7 +124,7 @@ fn debug_bytes<V: AsRef<[u8]>>(input: V) -> String {
if input_ref.len() <= 6 {
let mut ret = String::new();
for byte in input_ref.iter() {
write!(ret, "{:02x}", byte).unwrap_or(());
write!(ret, "{byte:02x}").unwrap_or(());
}
return ret;
}
Expand Down
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ pub fn encrypt(bytes: Bytes) -> Result<(DataMap, Vec<EncryptedChunk>)> {
let file_size = bytes.len();
if file_size < MIN_ENCRYPTABLE_BYTES {
return Err(Error::Generic(format!(
"Too small for self-encryption! Required size at least {}",
MIN_ENCRYPTABLE_BYTES
"Too small for self-encryption! Required size at least {MIN_ENCRYPTABLE_BYTES}",
)));
}

Expand Down Expand Up @@ -495,7 +494,7 @@ pub fn decrypt(data_map: &DataMap, chunks: &[EncryptedChunk]) -> Result<Bytes> {
chunk_map
.get(&hash)
.map(|chunk| chunk.content.clone())
.ok_or_else(|| Error::Generic(format!("Chunk not found for hash: {:?}", hash)))
.ok_or_else(|| Error::Generic(format!("Chunk not found for hash: {hash:?}")))
};

// Get the root map if we're dealing with a child map
Expand Down Expand Up @@ -588,8 +587,7 @@ where

if file_size < MIN_ENCRYPTABLE_BYTES {
return Err(Error::Generic(format!(
"Too small for self-encryption! Required size at least {}",
MIN_ENCRYPTABLE_BYTES
"Too small for self-encryption! Required size at least {MIN_ENCRYPTABLE_BYTES}",
)));
}

Expand Down Expand Up @@ -720,7 +718,10 @@ where
.iter()
.map(|info| {
let content = chunk_cache.get(&info.dst_hash).ok_or_else(|| {
Error::Generic(format!("Chunk not found for hash: {:?}", info.dst_hash))
Error::Generic(format!(
"Chunk not found for hash: {hash:?}",
hash = info.dst_hash
))
})?;
Ok(EncryptedChunk {
content: content.clone(),
Expand Down Expand Up @@ -751,7 +752,7 @@ where
///
/// * `Result<Vec<u8>>` - The serialized bytes or an error
pub fn serialize<T: serde::Serialize>(data: &T) -> Result<Vec<u8>> {
bincode::serialize(data).map_err(|e| Error::Generic(format!("Serialization error: {}", e)))
bincode::serialize(data).map_err(|e| Error::Generic(format!("Serialization error: {e}")))
}

/// Deserializes bytes into a data structure using bincode.
Expand All @@ -764,7 +765,7 @@ pub fn serialize<T: serde::Serialize>(data: &T) -> Result<Vec<u8>> {
///
/// * `Result<T>` - The deserialized data structure or an error
pub fn deserialize<T: serde::de::DeserializeOwned>(bytes: &[u8]) -> Result<T> {
bincode::deserialize(bytes).map_err(|e| Error::Generic(format!("Deserialization error: {}", e)))
bincode::deserialize(bytes).map_err(|e| Error::Generic(format!("Deserialization error: {e}")))
}

/// Verifies and deserializes a chunk by checking its content hash matches the provided name.
Expand All @@ -790,8 +791,7 @@ pub fn verify_chunk(name: XorName, bytes: &[u8]) -> Result<EncryptedChunk> {
// Verify the hash matches
if calculated_hash != name {
return Err(Error::Generic(format!(
"Chunk content hash mismatch. Expected: {:?}, Got: {:?}",
name, calculated_hash
"Chunk content hash mismatch. Expected: {name:?}, Got: {calculated_hash:?}"
)));
}

Expand Down Expand Up @@ -900,7 +900,7 @@ mod tests {

println!("\nFinal Data Map Info:");
println!("Number of chunks: {}", shrunk_map.len());
println!("Original file size: {}", file_size);
println!("Original file size: {file_size}");
println!("Is child: {}", shrunk_map.is_child());

for (i, info) in shrunk_map.infos().iter().enumerate() {
Expand Down
36 changes: 17 additions & 19 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl PyDataMap {
#[staticmethod]
pub fn from_json(json_str: &str) -> PyResult<Self> {
let inner = serde_json::from_str(json_str)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Invalid JSON: {}", e)))?;
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Invalid JSON: {e}")))?;
Ok(Self { inner })
}

Expand All @@ -146,7 +146,7 @@ impl PyDataMap {
/// str: A JSON string representation of the DataMap.
pub fn to_json(&self) -> PyResult<String> {
serde_json::to_string(&self.inner).map_err(|e| {
pyo3::exceptions::PyValueError::new_err(format!("Failed to serialize: {}", e))
pyo3::exceptions::PyValueError::new_err(format!("Failed to serialize: {e}"))
})
}

Expand Down Expand Up @@ -274,9 +274,8 @@ impl PyEncryptedChunk {
#[pyfunction]
pub fn encrypt(data: &[u8]) -> PyResult<(PyDataMap, Vec<PyEncryptedChunk>)> {
let bytes = Bytes::copy_from_slice(data);
let (data_map, chunks) = crate::encrypt(bytes).map_err(|e| {
pyo3::exceptions::PyValueError::new_err(format!("Encryption failed: {}", e))
})?;
let (data_map, chunks) = crate::encrypt(bytes)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Encryption failed: {e}")))?;
let py_chunks = chunks
.into_iter()
.map(|chunk| PyEncryptedChunk { inner: chunk })
Expand Down Expand Up @@ -307,9 +306,8 @@ pub fn decrypt(
.into_iter()
.map(|chunk| chunk.inner)
.collect::<Vec<_>>();
let bytes = crate::decrypt(&data_map.inner, &inner_chunks).map_err(|e| {
pyo3::exceptions::PyValueError::new_err(format!("Decryption failed: {}", e))
})?;
let bytes = crate::decrypt(&data_map.inner, &inner_chunks)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("Decryption failed: {e}")))?;
Ok(bytes.to_vec().into())
}

Expand All @@ -335,7 +333,7 @@ pub fn encrypt_from_file(input_file: &str, output_dir: &str) -> PyResult<(PyData
let input_path = Path::new(input_file);
let output_path = Path::new(output_dir);
let (data_map, chunk_names) = rust_encrypt_from_file(input_path, output_path).map_err(|e| {
pyo3::exceptions::PyOSError::new_err(format!("Failed to encrypt file: {}", e))
pyo3::exceptions::PyOSError::new_err(format!("Failed to encrypt file: {e}"))
})?;
let chunk_names = chunk_names.iter().map(|name| hex::encode(name.0)).collect();
Ok((PyDataMap { inner: data_map }, chunk_names))
Expand Down Expand Up @@ -365,15 +363,15 @@ pub fn decrypt_from_storage(
let name_str = hex::encode(name.0);
let chunk = get_chunk
.call1((name_str,))
.map_err(|e| crate::Error::Python(format!("Failed to call get_chunk: {}", e)))?;
.map_err(|e| crate::Error::Python(format!("Failed to call get_chunk: {e}")))?;
let bytes = chunk
.downcast::<PyBytes>()
.map_err(|e| crate::Error::Python(format!("get_chunk must return bytes: {}", e)))?;
.map_err(|e| crate::Error::Python(format!("get_chunk must return bytes: {e}")))?;
Ok(Bytes::copy_from_slice(bytes.as_bytes()))
};

rust_decrypt_from_storage(&data_map.inner, output_path, get_chunk_wrapper)
.map_err(|e| pyo3::exceptions::PyOSError::new_err(format!("Decryption failed: {}", e)))
.map_err(|e| pyo3::exceptions::PyOSError::new_err(format!("Decryption failed: {e}")))
}

/// Decrypt data using streaming for better performance with large files.
Expand All @@ -400,24 +398,24 @@ pub fn streaming_decrypt_from_storage(
let name_strs: Vec<String> = names.iter().map(|x| hex::encode(x.0)).collect();
let chunks = get_chunks
.call1((name_strs,))
.map_err(|e| crate::Error::Python(format!("Failed to call get_chunks: {}", e)))?;
.map_err(|e| crate::Error::Python(format!("Failed to call get_chunks: {e}")))?;
let chunks = chunks
.try_iter()
.map_err(|e| crate::Error::Python(format!("get_chunks must return a list: {}", e)))?;
.map_err(|e| crate::Error::Python(format!("get_chunks must return a list: {e}")))?;
let mut result = Vec::new();
for chunk in chunks {
let chunk = chunk
.map_err(|e| crate::Error::Python(format!("Failed to iterate chunks: {}", e)))?;
let bytes = chunk.downcast::<PyBytes>().map_err(|e| {
crate::Error::Python(format!("get_chunks must return bytes: {}", e))
})?;
.map_err(|e| crate::Error::Python(format!("Failed to iterate chunks: {e}")))?;
let bytes = chunk
.downcast::<PyBytes>()
.map_err(|e| crate::Error::Python(format!("get_chunks must return bytes: {e}")))?;
result.push(Bytes::copy_from_slice(bytes.as_bytes()));
}
Ok(result)
};

rust_streaming_decrypt_from_storage(&data_map.inner, output_path, get_chunks_wrapper).map_err(
|e| pyo3::exceptions::PyOSError::new_err(format!("Streaming decryption failed: {}", e)),
|e| pyo3::exceptions::PyOSError::new_err(format!("Streaming decryption failed: {e}")),
)
}

Expand Down
Loading