From 6185a3123a7666c8874934f9a384875bbd3a2c2d Mon Sep 17 00:00:00 2001 From: RafayAhmad7548 Date: Thu, 3 Sep 2026 13:00:40 +0500 Subject: [PATCH] refactor(utils): add hash verification functions --- src/commands/create.rs | 11 ++++------- src/utils.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/commands/create.rs b/src/commands/create.rs index 91a0a85..7f31ef1 100644 --- a/src/commands/create.rs +++ b/src/commands/create.rs @@ -2,7 +2,6 @@ use std::error::Error; use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme}; use futures_util::StreamExt; -use sha1::{Digest, Sha1}; use tokio::{fs::File, io::AsyncWriteExt}; use crate::{ @@ -11,7 +10,7 @@ use crate::{ fabric::FabricVersions, vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}, }, - utils::{with_progressbar, with_spinner}, + utils::{verify_sha1, with_progressbar, with_spinner}, }; pub struct Create { @@ -172,12 +171,10 @@ impl LoaderDownloader for VanillaDownloader { with_spinner("verifying sha1 hash", "sha1 hash verified", || async { let data = tokio::fs::read("server.jar").await?; - let mut hasher = Sha1::new(); - hasher.update(&data); - let hash = hasher.finalize(); - let hex: String = hash.iter().map(|b| format!("{:02x}", b)).collect(); - if hex != version_detail.downloads.server.sha1 { + let verified = verify_sha1(&data, &version_detail.downloads.server.sha1); + + if !verified { tokio::fs::remove_file("server.jar").await?; return Err("sha1 verifaction failed for downloaded file".into()); } diff --git a/src/utils.rs b/src/utils.rs index 8d855df..9f8e106 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -47,3 +47,15 @@ where result } + +pub fn verify_sha1(data: &[u8], expected_hex: &str) -> bool { + let mut hasher = Sha1::new(); + hasher.update(data); + hex::encode(hasher.finalize()) == expected_hex +} + +pub fn verify_sha512(data: &[u8], expected_hex: &str) -> bool { + let mut hasher = Sha512::new(); + hasher.update(data); + hex::encode(hasher.finalize()) == expected_hex +}