@@ -595,26 +595,8 @@ impl SecretScanner {
595595 hashset_string_in. is_subset ( & HashSet :: from_iter ( STANDARD_ENCODE . iter ( ) ) )
596596 }
597597
598- // from https://docs.rs/crate/entropy/0.3.0/source/src/lib.rs
599- // modified to include the keyspace parameter since we're not calculating against all possible
600- // byte values
601- fn calc_entropy ( bytes : & [ u8 ] , keyspace : i32 ) -> f32 {
602- let mut entropy = 0.0 ;
603- let mut counts: HashMap < u8 , i32 > = HashMap :: new ( ) ;
604-
605- for & b in bytes {
606- counts. insert ( b, counts. get ( & b) . unwrap_or ( & 0 ) + 1 ) ;
607- }
608-
609- for & count in counts. values ( ) {
610- let p: f32 = ( count as f32 ) / ( keyspace as f32 ) ;
611- entropy -= p * p. log ( 2.0 ) ;
612- }
613- //println!("{:?} {}", String::from_utf8(Vec::from(bytes)), entropy);
614- entropy
615- }
616598
617- /// Compute the Shannon entropy for a byte array
599+ /// Compute the Shannon entropy for a byte array (from https://docs.rs/crate/entropy/0.3.0/source/src/lib.rs)
618600 fn calc_shannon_entropy ( bytes : & [ u8 ] ) -> f32 {
619601 let mut entropy = 0.0 ;
620602 let mut counts: HashMap < u8 , i32 > = HashMap :: new ( ) ;
@@ -655,14 +637,14 @@ impl SecretScanner {
655637 let mut b64_words: Vec < String > = words
656638 . iter ( )
657639 . filter ( |word| word. len ( ) >= 20 && Self :: is_base64_string ( word) )
658- . filter ( |word| Self :: calc_entropy ( word, 64 ) > 4.5 )
640+ . filter ( |word| Self :: calc_shannon_entropy ( word) > 4.5 )
659641 . map ( |word| str:: from_utf8 ( word) . unwrap ( ) . to_string ( ) )
660642 . collect ( ) ;
661643 let mut hex_words: Vec < String > = words
662644 . iter ( ) // there must be a better way
663645 . filter ( |word| ( word. len ( ) >= 20 ) && ( word. iter ( ) . all ( u8:: is_ascii_hexdigit) ) )
664646 . filter_map ( |& x| hex:: decode ( x) . ok ( ) )
665- . filter ( |word| Self :: calc_entropy ( word, 255 ) > ( 3_f32 ) )
647+ . filter ( |word| Self :: calc_shannon_entropy ( word) > 4.5 )
666648 . map ( hex:: encode)
667649 . collect ( ) ;
668650 let mut output: Vec < String > = Vec :: new ( ) ;
0 commit comments