@@ -10,6 +10,7 @@ use sha1::Sha1;
1010use sha2:: { Sha224 , Sha256 , Sha384 , Sha512 } ;
1111use sha3:: { Sha3_224 , Sha3_256 , Sha3_384 , Sha3_512 } ;
1212use std:: {
13+ fmt:: Write as WriteStr ,
1314 fs:: File ,
1415 io:: Write ,
1516 path:: Path ,
@@ -105,7 +106,7 @@ where
105106 println ! ( "Please enter a valid number greater than 0" ) ;
106107 }
107108 }
108- Err ( e) => println ! ( "Please enter a valid number greater than 0, {}" , e ) ,
109+ Err ( e) => println ! ( "Please enter a valid number greater than 0, {e}" ) ,
109110 }
110111 }
111112
@@ -283,7 +284,7 @@ pub fn save_passwd_to_file(file: &Arc<Mutex<File>>, passwords: &str) -> Result<(
283284 ) )
284285 } ) ?;
285286
286- file. write_all ( format ! ( "{}\n " , passwords ) . as_bytes ( ) )
287+ file. write_all ( format ! ( "{passwords }\n " ) . as_bytes ( ) )
287288 . map_err ( |_| {
288289 WorgenXError :: SystemError ( SystemError :: UnableToWriteToFile (
289290 "output file" . to_string ( ) ,
@@ -331,20 +332,20 @@ pub fn get_estimated_size(nb_of_passwords: u64, length: u64) -> String {
331332 let size: u64 = nb_of_passwords * ( length + 1 ) ; // +1 for the newline character
332333 let mut size_str: String = String :: new ( ) ;
333334 if size < 1024 {
334- size_str . push_str ( & size. to_string ( ) ) ;
335- size_str . push_str ( " bytes" ) ;
335+ write ! ( size_str , "{ size}" ) . unwrap_or_default ( ) ;
336+ write ! ( size_str , " bytes" ) . unwrap_or_default ( ) ;
336337 } else if size < 1048576 {
337- size_str . push_str ( & format ! ( "{:.2}" , size as f64 / 1024.0 ) ) ;
338- size_str . push_str ( " KB" ) ;
338+ write ! ( size_str , "{:.2}" , size as f64 / 1024.0 ) . unwrap_or_default ( ) ;
339+ write ! ( size_str , " KB" ) . unwrap_or_default ( ) ;
339340 } else if size < 1073741824 {
340- size_str . push_str ( & format ! ( "{:.2}" , size as f64 / 1048576.0 ) ) ;
341- size_str . push_str ( " MB" ) ;
341+ write ! ( size_str , "{:.2}" , size as f64 / 1048576.0 ) . unwrap_or_default ( ) ;
342+ write ! ( size_str , " MB" ) . unwrap_or_default ( ) ;
342343 } else if size < 1099511627776 {
343- size_str . push_str ( & format ! ( "{:.2}" , size as f64 / 1073741824.0 ) ) ;
344- size_str . push_str ( " GB" ) ;
344+ write ! ( size_str , "{:.2}" , size as f64 / 1073741824.0 ) . unwrap_or_default ( ) ;
345+ write ! ( size_str , " GB" ) . unwrap_or_default ( ) ;
345346 } else {
346- size_str . push_str ( & format ! ( "{:.2}" , size as f64 / 1099511627776.0 ) ) ;
347- size_str . push_str ( " TB" ) ;
347+ write ! ( size_str , "{:.2}" , size as f64 / 1099511627776.0 ) . unwrap_or_default ( ) ;
348+ write ! ( size_str , " TB" ) . unwrap_or_default ( ) ;
348349 }
349350 size_str
350351}
@@ -399,6 +400,36 @@ fn hash_with_digest<D: Digest>(mut hasher: D, password: &str) -> String {
399400 hex:: encode ( result)
400401}
401402
403+ /// This function returns the size of the hash in bytes for a given hash algorithm.
404+ /// It is used to determine the size of the hash in bytes.
405+ ///
406+ /// # Arguments
407+ ///
408+ /// * `hash` - The hash algorithm to check.
409+ ///
410+ /// # Returns
411+ ///
412+ /// The size of the hash in bytes. If the hash algorithm is not supported, it returns 0.
413+ ///
414+ pub fn get_size_of_hash ( hash : & str ) -> usize {
415+ match hash {
416+ "md5" => 32 ,
417+ "sha1" => 40 ,
418+ "sha224" => 56 ,
419+ "sha256" => 64 ,
420+ "sha384" => 96 ,
421+ "sha512" => 128 ,
422+ "sha3-224" => 56 ,
423+ "sha3-256" => 64 ,
424+ "sha3-384" => 96 ,
425+ "sha3-512" => 128 ,
426+ "blake2b-512" => 128 ,
427+ "blake2s-256" => 64 ,
428+ "whirlpool" => 128 ,
429+ _ => 0 , // Unsupported hash algorithm.
430+ }
431+ }
432+
402433#[ cfg( test) ]
403434mod tests {
404435 use super :: * ;
@@ -509,4 +540,22 @@ mod tests {
509540 assert_eq ! ( manage_hash( password, "whirlpool" ) . unwrap( ) , "74dfc2b27acfa364da55f93a5caee29ccad3557247eda238831b3e9bd931b01d77fe994e4f12b9d4cfa92a124461d2065197d8cf7f33fc88566da2db2a4d6eae" ) ;
510541 assert ! ( manage_hash( password, "sha999" ) . is_err( ) ) ;
511542 }
543+
544+ #[ test]
545+ fn test_get_size_of_hash ( ) {
546+ assert_eq ! ( get_size_of_hash( "md5" ) , 32 ) ;
547+ assert_eq ! ( get_size_of_hash( "sha1" ) , 40 ) ;
548+ assert_eq ! ( get_size_of_hash( "sha224" ) , 56 ) ;
549+ assert_eq ! ( get_size_of_hash( "sha256" ) , 64 ) ;
550+ assert_eq ! ( get_size_of_hash( "sha384" ) , 96 ) ;
551+ assert_eq ! ( get_size_of_hash( "sha512" ) , 128 ) ;
552+ assert_eq ! ( get_size_of_hash( "sha3-224" ) , 56 ) ;
553+ assert_eq ! ( get_size_of_hash( "sha3-256" ) , 64 ) ;
554+ assert_eq ! ( get_size_of_hash( "sha3-384" ) , 96 ) ;
555+ assert_eq ! ( get_size_of_hash( "sha3-512" ) , 128 ) ;
556+ assert_eq ! ( get_size_of_hash( "blake2b-512" ) , 128 ) ;
557+ assert_eq ! ( get_size_of_hash( "blake2s-256" ) , 64 ) ;
558+ assert_eq ! ( get_size_of_hash( "whirlpool" ) , 128 ) ;
559+ assert_eq ! ( get_size_of_hash( "sha999" ) , 0 ) ; // Unsupported hash algorithm
560+ }
512561}
0 commit comments