From 8ce1155a1eb490625a7e949a10c4283b4b773d30 Mon Sep 17 00:00:00 2001 From: Kim Altintop Date: Thu, 13 Apr 2023 16:50:24 +0200 Subject: core: replace bundle checksum with BLAKE3 BLAKE3 has a verified streaming mode (Bao), which allows variable chunk sizes without altering the root hash. This means that a BLAKE3 hash can serve as a long-term stable content address in location-independent storage (as demonstrated by iroh). Signed-off-by: Kim Altintop --- src/patches/bundle.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'src/patches/bundle.rs') diff --git a/src/patches/bundle.rs b/src/patches/bundle.rs index 296b24a..52397a1 100644 --- a/src/patches/bundle.rs +++ b/src/patches/bundle.rs @@ -21,11 +21,9 @@ use anyhow::{ ensure, Context, }; +use digest::Digest; use multipart::client::lazy::Multipart; -use sha2::{ - Digest, - Sha256, -}; +use sha2::Sha256; use tempfile::NamedTempFile; use url::Url; @@ -105,14 +103,14 @@ impl Bundle { let encryption = pack.encryption()?; drop(pack); let mut file = File::open(&path)?; - let mut sha2 = Sha256::new(); + let mut hasher = blake3::Hasher::new(); - let len = io::copy(&mut file, &mut sha2)?; + let len = io::copy(&mut file, &mut hasher)?; let hash = header.hash(); ensure!(expect.hash == &hash, "header hash mismatch"); - let checksum = sha2.finalize().into(); + let checksum = bundle::Checksum::from(&hasher); if let Some(expect) = expect.checksum { - ensure!(expect == checksum, "claimed and actual hash differ"); + ensure!(expect == &checksum, "claimed and actual hash differ"); } let info = bundle::Info { @@ -138,10 +136,10 @@ impl Bundle { { std::fs::create_dir_all(&to)?; let mut tmp = NamedTempFile::new_in(&to)?; - let mut out = HashWriter::new(Sha256::new(), &mut tmp); + let mut out = HashWriter::new(blake3::Hasher::new(), &mut tmp); let len = io::copy(&mut from, &mut out)?; - let checksum = out.hash().into(); + let checksum = bundle::Checksum::from(out.hasher()); let (header, mut pack) = split(tmp.path())?; let hash = header.hash(); -- cgit v1.2.3