@@ -12,7 +12,6 @@ use flate2::read::GzDecoder;
1212use indicatif:: { ProgressBar , ProgressStyle } ;
1313use once_cell:: sync:: Lazy ;
1414use progress_reader:: ProgressReader ;
15- use sha2:: { Digest , Sha256 } ;
1615use std:: fs;
1716use std:: io:: { BufReader , BufWriter , Read } ;
1817use std:: path:: { Path , PathBuf } ;
@@ -57,8 +56,8 @@ pub enum Error {
5756 #[ error( transparent) ]
5857 FailedDownload ( #[ from] curl:: Error ) ,
5958
60- #[ error( "Invalid checksum \n exptected: {expected} \n got: {got}" ) ]
61- InvalidChecksum { expected : String , got : String } ,
59+ #[ error( transparent ) ]
60+ InvalidChecksum ( # [ from ] release :: ChecksumError ) ,
6261
6362 #[ error( transparent) ]
6463 FailedMake ( #[ from] make:: Error ) ,
@@ -171,37 +170,16 @@ fn download(url: &str, dir: impl AsRef<Path>) -> Result<PathBuf, Error> {
171170
172171fn verify ( filepath : impl AsRef < Path > , checksum : Option < & Hash > ) -> Result < ( ) , Error > {
173172 if let Some ( checksum) = checksum {
173+ let hash_type = checksum. hash_type ( ) ;
174174 let file = fs:: File :: open ( filepath) ?;
175175 let progress_bar = ProgressBar :: new ( file. metadata ( ) ?. len ( ) )
176176 . with_style ( PROGRESS_STYLE . clone ( ) )
177- . with_prefix ( "Verifying" ) ;
178- let mut file_reader = ProgressReader :: new ( file, & progress_bar) ;
179-
180- let ( checksum, hash, hash_type) = match checksum {
181- Hash :: SHA256 ( checksum) => {
182- progress_bar. set_message ( "SHA-256 checksum" ) ;
183- let mut sha256 = Sha256 :: new ( ) ;
184- std:: io:: copy ( & mut file_reader, & mut sha256) ?;
185- let hash = sha256. finalize ( ) ;
186- ( checksum, format ! ( "{:x}" , hash) , "SHA-256" )
187- }
188- Hash :: MD5 ( checksum) => {
189- progress_bar. set_message ( "MD5 checksum" ) ;
190- let mut md5 = md5:: Context :: new ( ) ;
191- std:: io:: copy ( & mut file_reader, & mut md5) ?;
192- let hash = md5. compute ( ) ;
193- ( checksum, format ! ( "{:x}" , hash) , "MD5" )
194- }
195- } ;
196- if checksum == & hash {
197- progress_bar. finish_and_clear ( ) ;
198- println ! ( "{:>12} {} checksum" , "Verified" . green( ) . bold( ) , hash_type) ;
199- } else {
200- return Err ( Error :: InvalidChecksum {
201- expected : checksum. clone ( ) ,
202- got : hash,
203- } ) ;
204- }
177+ . with_prefix ( "Verifying" )
178+ . with_message ( hash_type) ;
179+
180+ checksum. verify ( ProgressReader :: new ( file, & progress_bar) ) ?;
181+ progress_bar. finish_and_clear ( ) ;
182+ println ! ( "{:>12} {} checksum" , "Verified" . green( ) . bold( ) , hash_type) ;
205183 } else {
206184 println ! (
207185 "{:>12} {}: No checksum" ,
0 commit comments