@@ -692,7 +692,7 @@ fn identify_algo_name_and_length(
692692 line_info : & LineInfo ,
693693 algo_name_input : Option < & str > ,
694694 last_algo : & mut Option < String > ,
695- ) -> Option < ( String , Option < usize > ) > {
695+ ) -> Result < ( String , Option < usize > ) , LineCheckError > {
696696 let algo_from_line = line_info. algo_name . clone ( ) . unwrap_or_default ( ) ;
697697 let algorithm = algo_from_line. to_lowercase ( ) ;
698698 * last_algo = Some ( algo_from_line) ;
@@ -701,18 +701,25 @@ fn identify_algo_name_and_length(
701701 // (for example SHA1 (f) = d...)
702702 // Also handle the case cksum -s sm3 but the file contains other formats
703703 if algo_name_input. is_some ( ) && algo_name_input != Some ( & algorithm) {
704- return None ;
704+ return Err ( LineCheckError :: ImproperlyFormatted ) ;
705705 }
706706
707707 if !SUPPORTED_ALGORITHMS . contains ( & algorithm. as_str ( ) ) {
708708 // Not supported algo, leave early
709- return None ;
709+ return Err ( LineCheckError :: ImproperlyFormatted ) ;
710710 }
711711
712712 let bytes = if let Some ( bitlen) = line_info. algo_bit_len {
713- if bitlen % 8 != 0 {
714- // The given length is wrong
715- return None ;
713+ if algorithm != ALGORITHM_OPTIONS_BLAKE2B || bitlen % 8 != 0 {
714+ // Either
715+ // the algo based line is provided with a bit length
716+ // with an algorithm that does not support it (only Blake2B does).
717+ //
718+ // eg: MD5-128 (foo.txt) = fffffffff
719+ // ^ This is illegal
720+ // OR
721+ // the given length is wrong because it's not a multiple of 8.
722+ return Err ( LineCheckError :: ImproperlyFormatted ) ;
716723 }
717724 Some ( bitlen / 8 )
718725 } else if algorithm == ALGORITHM_OPTIONS_BLAKE2B {
@@ -722,7 +729,7 @@ fn identify_algo_name_and_length(
722729 None
723730 } ;
724731
725- Some ( ( algorithm, bytes) )
732+ Ok ( ( algorithm, bytes) )
726733}
727734
728735/// Given a filename and an algorithm, compute the digest and compare it with
@@ -773,8 +780,7 @@ fn process_algo_based_line(
773780 let filename_to_check = line_info. filename . as_slice ( ) ;
774781
775782 let ( algo_name, algo_byte_len) =
776- identify_algo_name_and_length ( line_info, cli_algo_name, last_algo)
777- . ok_or ( LineCheckError :: ImproperlyFormatted ) ?;
783+ identify_algo_name_and_length ( line_info, cli_algo_name, last_algo) ?;
778784
779785 // If the digest bitlen is known, we can check the format of the expected
780786 // checksum with it.
0 commit comments