From 26f7d8587831558eff0ed8d651a0b1b73f02fd19 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sat, 20 Jul 2024 10:49:36 +0200 Subject: [PATCH] replace sh1 file hashes with blake3 --- Cargo.lock | 39 ++++++++++++++++++++++++++++++++------- Cargo.toml | 2 +- src/main.rs | 25 +++++++++++++++++-------- src/misc.rs | 16 ++++++++++++---- src/structs.rs | 2 +- 5 files changed, 63 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46b0289..674d3aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" name = "alterware-launcher" version = "0.7.0" dependencies = [ + "blake3", "colored", "futures-util", "indicatif", @@ -33,7 +34,6 @@ dependencies = [ "semver", "serde", "serde_json", - "sha1_smol", "simple-log", "static_vcruntime", "steamlocate", @@ -62,6 +62,18 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dabe5a181f83789739c194cbe5a897dde195078fac08568d09221fd6137a7ba8" +[[package]] +name = "arrayref" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "autocfg" version = "1.1.0" @@ -101,6 +113,19 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" +[[package]] +name = "blake3" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9ec96fe9a81b5e365f9db71fe00edc4fe4ca2cc7dcb7861f0603012a7caa210" +dependencies = [ + "arrayref", + "arrayvec", + "cc", + "cfg-if", + "constant_time_eq", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -177,6 +202,12 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "constant_time_eq" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" + [[package]] name = "convert_case" version = "0.5.0" @@ -1468,12 +1499,6 @@ dependencies = [ "yaml-rust", ] -[[package]] -name = "sha1_smol" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" - [[package]] name = "sha2" version = "0.10.7" diff --git a/Cargo.toml b/Cargo.toml index 449d3db..702b107 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ panic = "abort" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -sha1_smol = "1.0.0" +blake3 = "1.5.3" serde = { version = "1.0.204", features = ["derive"] } serde_json = "1.0.120" rand = "0.8.5" diff --git a/src/main.rs b/src/main.rs index 142b731..3ac7c79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,17 +202,17 @@ async fn update_dir( continue; } - let sha1_remote = file.hash.to_lowercase(); + let hash_remote = file.blake3.to_lowercase(); let file_name = &file.name.replace(remote_dir_pre.as_str(), ""); let file_path = dir.join(file_name); if file_path.exists() { - let sha1_local = hashes + let hash_local = hashes .get(file_name) .map(Cow::Borrowed) - .unwrap_or_else(|| Cow::Owned(misc::get_file_sha1(&file_path))) + .unwrap_or_else(|| Cow::Owned(misc::file_blake3(&file_path).unwrap())) .to_string(); - if sha1_local != sha1_remote { + if hash_local != hash_remote { files_to_download.push(file.clone()); } else { let msg = format!( @@ -222,7 +222,7 @@ async fn update_dir( ); pb.println(&msg); info!("{}", msg); - hashes.insert(file_name.to_owned(), file.hash.to_lowercase()); + hashes.insert(file_name.to_owned(), file.blake3.to_lowercase()); } } else { files_to_download.push(file.clone()); @@ -269,7 +269,7 @@ async fn update_dir( { panic!("{err}"); }; - let hash = misc::get_file_sha1(&file_path); + let hash = misc::file_blake3(&file_path).unwrap(); hashes.insert(file_name.to_owned(), hash.to_lowercase()); #[cfg(unix)] if file_name.ends_with(".exe") { @@ -308,8 +308,17 @@ async fn update( std::process::exit(0); } + if dir.join(".sha-sums").exists() { + match fs::remove_file(dir.join(".sha-sums")) { + Ok(_) => {} + Err(error) => { + crate::println_error!("Error removing .sha-sums: {:#?}", error); + } + } + } + let mut hashes = HashMap::new(); - let hash_file = dir.join(".sha-sums"); + let hash_file = dir.join(".hashes"); if hash_file.exists() && !force { let hash_file = fs::read_to_string(hash_file).unwrap(); for line in hash_file.lines() { @@ -445,7 +454,7 @@ async fn update( for (file, hash) in hashes.iter() { hash_file_content.push_str(&format!("{} {}\n", hash, file)); } - fs::write(dir.join(".sha-sums"), hash_file_content).unwrap(); + fs::write(dir.join(".hashes"), hash_file_content).unwrap(); } #[cfg(windows)] diff --git a/src/misc.rs b/src/misc.rs index 40c2e12..686ef20 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -6,10 +6,18 @@ use std::{ use colored::Colorize; use indicatif::{ProgressBar, ProgressStyle}; -pub fn get_file_sha1(path: &PathBuf) -> String { - let mut sha1 = sha1_smol::Sha1::new(); - sha1.update(&fs::read(path).unwrap()); - sha1.digest().to_string() +pub fn file_blake3(file: &std::path::Path) -> std::io::Result { + let mut blake3 = blake3::Hasher::new(); + let mut file = std::fs::File::open(file)?; + let mut buffer = [0; 1024]; + loop { + let n = std::io::Read::read(&mut file, &mut buffer)?; + if n == 0 { + break; + } + blake3.update(&buffer[..n]); + } + Ok(blake3.finalize().to_string()) } pub fn stdin() -> String { diff --git a/src/structs.rs b/src/structs.rs index dba96b4..8cf7cfe 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -4,7 +4,7 @@ use std::path::Path; pub struct CdnFile { pub name: String, pub size: u32, - pub hash: String, + pub blake3: String, } #[derive(serde::Deserialize, serde::Serialize)]