Function mega::compute_sparse_checksum
source · pub async fn compute_sparse_checksum<R: AsyncRead>(
reader: R,
size: u64
) -> Result<[u8; 16]>Expand description
This function computes a sparse CRC32-based checksum, in the exact same way that MEGA does it.
This allows to compute a checksum for any arbitrary data and compare it to the ones of remote MEGA nodes.
Please be aware that, due to these checksums being sparse, two identical checksums can be identical to one another despite being generated from very slightly different files.
Using condensed MACs is more accurate to assess file integrity than the sparse checksum method, but it is both more CPU and disk intensive to do so.
Here is an example of how to use this function:
use tokio_util::compat::TokioAsyncReadCompatExt;
let nodes = mega.fetch_own_nodes().await?;
let remote_checksum = {
let node = nodes.get_node_by_path("/Root/some-remote-file.txt").unwrap();
node.sparse_checksum().unwrap()
};
let local_checksum = {
let file = tokio::fs::File::open("some-local-file.txt").await?;
let size = file.metadata().await?.len();
mega::compute_sparse_checksum(file.compat(), size).await?
};
if local_checksum == *remote_checksum {
println!("OK ! (the checksums are identical)");
} else {
println!("FAILED ! (the checksums differ)");
}