@@ -15,7 +15,7 @@ use std::io::{self, BufReader, Read, Write, stderr, stdin};
1515use os_display:: Quotable ;
1616
1717use crate :: checksum:: {
18- AlgoKind , BlakeLength , ChecksumError , ReadingMode , SizedAlgoKind , digest_reader,
18+ AlgoKind , BlakeLength , ChecksumError , ReadingMode , ShaLength , SizedAlgoKind , digest_reader,
1919 parse_blake_length, unescape_filename,
2020} ;
2121use crate :: error:: { FromIo , UError , UIoError , UResult , USimpleError } ;
@@ -490,14 +490,16 @@ impl LineInfo {
490490}
491491
492492/// Extract the expected digest from the checksum string and decode it
493- fn get_raw_expected_digest ( checksum : & str , byte_len_hint : Option < usize > ) -> Option < Vec < u8 > > {
493+ fn get_raw_expected_digest ( checksum : & str , bit_len_hint : Option < usize > ) -> Option < Vec < u8 > > {
494494 // If the length of the digest is not a multiple of 2, then it must be
495495 // improperly formatted (1 byte is 2 hex digits, and base64 strings should
496496 // always be a multiple of 4).
497497 if !checksum. len ( ) . is_multiple_of ( 2 ) {
498498 return None ;
499499 }
500500
501+ let byte_len_hint = bit_len_hint. map ( |n| n. div_ceil ( 8 ) ) ;
502+
501503 let checks_hint = |len| byte_len_hint. is_none_or ( |hint| hint == len) ;
502504
503505 // If the length of the string matches the one to be expected (in case it's
@@ -741,23 +743,23 @@ fn process_algo_based_line(
741743) -> Result < ( ) , LineCheckError > {
742744 let filename_to_check = line_info. filename . as_slice ( ) ;
743745
744- let ( algo_kind, algo_byte_len) =
745- identify_algo_name_and_length ( line_info, cli_algo_kind, last_algo) ?;
746+ let ( algo_kind, algo_len) = identify_algo_name_and_length ( line_info, cli_algo_kind, last_algo) ?;
746747
747748 // If the digest bitlen is known, we can check the format of the expected
748749 // checksum with it.
749- let digest_char_length_hint = match ( algo_kind, algo_byte_len ) {
750- ( AlgoKind :: Blake2b | AlgoKind :: Blake3 , Some ( byte_len) ) => Some ( byte_len) ,
751- ( AlgoKind :: Shake128 | AlgoKind :: Shake256 , Some ( bit_len) ) => Some ( bit_len. div_ceil ( 8 ) ) ,
752- ( AlgoKind :: Shake128 , None ) => Some ( sum:: Shake128 :: DEFAULT_BIT_SIZE . div_ceil ( 8 ) ) ,
753- ( AlgoKind :: Shake256 , None ) => Some ( sum:: Shake256 :: DEFAULT_BIT_SIZE . div_ceil ( 8 ) ) ,
750+ let digest_bit_length_hint = match ( algo_kind, algo_len ) {
751+ ( AlgoKind :: Blake2b | AlgoKind :: Blake3 , Some ( byte_len) ) => Some ( byte_len * 8 ) ,
752+ ( AlgoKind :: Shake128 | AlgoKind :: Shake256 , Some ( bit_len) ) => Some ( bit_len) ,
753+ ( AlgoKind :: Shake128 , None ) => Some ( sum:: Shake128 :: DEFAULT_BIT_SIZE ) ,
754+ ( AlgoKind :: Shake256 , None ) => Some ( sum:: Shake256 :: DEFAULT_BIT_SIZE ) ,
754755 _ => None ,
755756 } ;
756757
757- let expected_checksum = get_raw_expected_digest ( & line_info. checksum , digest_char_length_hint )
758+ let expected_checksum = get_raw_expected_digest ( & line_info. checksum , digest_bit_length_hint )
758759 . ok_or ( LineCheckError :: ImproperlyFormatted ) ?;
759760
760- let algo = SizedAlgoKind :: from_unsized ( algo_kind, algo_byte_len) ?;
761+ let algo = SizedAlgoKind :: from_unsized ( algo_kind, algo_len)
762+ . map_err ( |_| LineCheckError :: ImproperlyFormatted ) ?;
761763
762764 compute_and_check_digest_from_file ( filename_to_check, & expected_checksum, algo, opts)
763765}
@@ -779,7 +781,9 @@ fn process_non_algo_based_line(
779781 // Remove the leading asterisk if present - only for the first line
780782 filename_to_check = & filename_to_check[ 1 ..] ;
781783 }
782- let expected_checksum = get_raw_expected_digest ( & line_info. checksum , None )
784+
785+ let expected_digest_sum = cli_algo_kind. expected_digest_bit_len ( ) ;
786+ let expected_checksum = get_raw_expected_digest ( & line_info. checksum , expected_digest_sum)
783787 . ok_or ( LineCheckError :: ImproperlyFormatted ) ?;
784788
785789 // When a specific algorithm name is input, use it and use the provided
@@ -789,7 +793,11 @@ fn process_non_algo_based_line(
789793 ak:: Blake2b | ak:: Blake3 => Some ( expected_checksum. len ( ) ) ,
790794 ak:: Sha2 | ak:: Sha3 => {
791795 // multiplication by 8 to get the number of bits
792- Some ( expected_checksum. len ( ) * 8 )
796+ Some (
797+ ShaLength :: try_from ( expected_checksum. len ( ) * 8 )
798+ . map_err ( |_| LineCheckError :: ImproperlyFormatted ) ?
799+ . as_usize ( ) ,
800+ )
793801 }
794802 _ => cli_algo_length,
795803 } ;
0 commit comments