download wix and verify with reqwest

This commit is contained in:
tensor-programming 2019-07-28 14:26:11 -04:00
parent 37936b018c
commit ba5754420b
3 changed files with 813 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -35,6 +35,8 @@ sha2 = "0.8"
lazy_static = "1.3"
handlebars = "1.1"
slog = "2.5.2"
reqwest = "0.9.19"
hex = "0.3"
[dev-dependencies]
tempfile = "3"

View File

@ -32,3 +32,29 @@ lazy_static! {
handlebars
};
}
fn download_and_verify(logger: &slog::Logger, url: &str, hash: &str) -> Result<Vec<u8>, String> {
info!(logger, "Downloading {}", url);
let mut response = reqwest::get(url).or_else(|e| Err(e.to_string()))?;
let mut data: Vec<u8> = Vec::new();
response
.read_to_end(&mut data)
.or_else(|e| Err(e.to_string()))?;
info!(logger, "validating hash...");
let mut hasher = sha2::Sha256::new();
hasher.input(&data);
let url_hash = hasher.result().to_vec();
let expected_hash = hex::decode(hash).or_else(|e| Err(e.to_string()))?;
if expected_hash == url_hash {
Ok(data)
} else {
Err("hash mismatch of downloaded file".to_string())
}
}