refactor(utils): add hash verification functions
This commit is contained in:
parent
51722966e4
commit
6185a3123a
2 changed files with 16 additions and 7 deletions
|
|
@ -2,7 +2,6 @@ use std::error::Error;
|
||||||
|
|
||||||
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
|
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
use sha1::{Digest, Sha1};
|
|
||||||
use tokio::{fs::File, io::AsyncWriteExt};
|
use tokio::{fs::File, io::AsyncWriteExt};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
@ -11,7 +10,7 @@ use crate::{
|
||||||
fabric::FabricVersions,
|
fabric::FabricVersions,
|
||||||
vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions},
|
vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions},
|
||||||
},
|
},
|
||||||
utils::{with_progressbar, with_spinner},
|
utils::{verify_sha1, with_progressbar, with_spinner},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Create {
|
pub struct Create {
|
||||||
|
|
@ -172,12 +171,10 @@ impl LoaderDownloader for VanillaDownloader {
|
||||||
|
|
||||||
with_spinner("verifying sha1 hash", "sha1 hash verified", || async {
|
with_spinner("verifying sha1 hash", "sha1 hash verified", || async {
|
||||||
let data = tokio::fs::read("server.jar").await?;
|
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?;
|
tokio::fs::remove_file("server.jar").await?;
|
||||||
return Err("sha1 verifaction failed for downloaded file".into());
|
return Err("sha1 verifaction failed for downloaded file".into());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/utils.rs
12
src/utils.rs
|
|
@ -47,3 +47,15 @@ where
|
||||||
|
|
||||||
result
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue