@@ -4,13 +4,15 @@ use crate::error::Error::{AssetHashNotFound, AssetNotFound, ReleaseNotFound, Une
44use crate :: error:: Result ;
55use crate :: github:: { Asset , Release } ;
66use crate :: version:: Version ;
7+ use crate :: Error :: ArchiveHashMismatch ;
78use bytes:: Bytes ;
89use flate2:: bufread:: GzDecoder ;
910use human_bytes:: human_bytes;
1011use num_format:: { Locale , ToFormattedString } ;
1112use regex:: Regex ;
1213use reqwest:: header:: HeaderMap ;
1314use reqwest:: { header, RequestBuilder } ;
15+ use sha2:: { Digest , Sha256 } ;
1416use std:: fs:: { create_dir_all, File } ;
1517use std:: io:: { copy, BufReader , Cursor } ;
1618use std:: path:: Path ;
@@ -179,8 +181,8 @@ async fn get_asset<S: AsRef<str>>(version: &Version, target: S) -> Result<(Versi
179181/// If the [version](Version) is not found for this target, then an
180182/// [error](crate::error::Error) is returned.
181183///
182- /// Returns the archive bytes and the archive hash .
183- pub async fn get_archive ( version : & Version ) -> Result < ( Version , Bytes , String ) > {
184+ /// Returns the archive version and bytes .
185+ pub async fn get_archive ( version : & Version ) -> Result < ( Version , Bytes ) > {
184186 get_archive_for_target ( version, target_triple:: TARGET ) . await
185187}
186188
@@ -189,11 +191,11 @@ pub async fn get_archive(version: &Version) -> Result<(Version, Bytes, String)>
189191/// If the [version](Version) or [target](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
190192/// is not found, then an [error](crate::error::Error) is returned.
191193///
192- /// Returns the archive bytes and the archive hash .
194+ /// Returns the archive version and bytes .
193195pub async fn get_archive_for_target < S : AsRef < str > > (
194196 version : & Version ,
195197 target : S ,
196- ) -> Result < ( Version , Bytes , String ) > {
198+ ) -> Result < ( Version , Bytes ) > {
197199 let ( asset_version, asset, asset_hash) = get_asset ( version, target) . await ?;
198200
199201 debug ! (
@@ -229,7 +231,15 @@ pub async fn get_archive_for_target<S: AsRef<str>>(
229231 human_bytes( archive. len( ) as f64 )
230232 ) ;
231233
232- Ok ( ( asset_version, archive, hash) )
234+ let mut hasher = Sha256 :: new ( ) ;
235+ hasher. update ( & archive) ;
236+ let archive_hash = hex:: encode ( hasher. finalize ( ) ) ;
237+
238+ if archive_hash != hash {
239+ return Err ( ArchiveHashMismatch { archive_hash, hash } ) ;
240+ }
241+
242+ Ok ( ( asset_version, archive) )
233243}
234244
235245/// Extracts the compressed tar [bytes](Bytes) to the [out_dir](Path).
0 commit comments