This repository was archived by the owner on Nov 30, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +34
-93
lines changed Expand file tree Collapse file tree 5 files changed +34
-93
lines changed Original file line number Diff line number Diff line change @@ -64,29 +64,7 @@ impl EngineTrait for HashEngine {
6464
6565 const BLOCK_SIZE : usize = 64 ;
6666
67- #[ cfg( not( feature = "fuzztarget" ) ) ]
68- fn input ( & mut self , mut inp : & [ u8 ] ) {
69- while !inp. is_empty ( ) {
70- let buf_idx = self . length % <Self as EngineTrait >:: BLOCK_SIZE ;
71- let rem_len = <Self as EngineTrait >:: BLOCK_SIZE - buf_idx;
72- let write_len = cmp:: min ( rem_len, inp. len ( ) ) ;
73-
74- self . buffer [ buf_idx..buf_idx + write_len] . copy_from_slice ( & inp[ ..write_len] ) ;
75- self . length += write_len;
76- if self . length % <Self as EngineTrait >:: BLOCK_SIZE == 0 {
77- self . process_block ( ) ;
78- }
79- inp = & inp[ write_len..] ;
80- }
81- }
82-
83- #[ cfg( feature = "fuzztarget" ) ]
84- fn input ( & mut self , inp : & [ u8 ] ) {
85- for c in inp {
86- self . buffer [ 0 ] ^= * c;
87- }
88- self . length += inp. len ( ) ;
89- }
67+ engine_input_impl ! ( ) ;
9068}
9169
9270/// Output of the RIPEMD160 hash function
Original file line number Diff line number Diff line change @@ -51,29 +51,8 @@ impl EngineTrait for HashEngine {
5151
5252
5353 const BLOCK_SIZE : usize = 64 ;
54- #[ cfg( not( feature = "fuzztarget" ) ) ]
55- fn input ( & mut self , mut inp : & [ u8 ] ) {
56- while !inp. is_empty ( ) {
57- let buf_idx = self . length % <Self as EngineTrait >:: BLOCK_SIZE ;
58- let rem_len = <Self as EngineTrait >:: BLOCK_SIZE - buf_idx;
59- let write_len = cmp:: min ( rem_len, inp. len ( ) ) ;
60-
61- self . buffer [ buf_idx..buf_idx + write_len] . copy_from_slice ( & inp[ ..write_len] ) ;
62- self . length += write_len;
63- if self . length % <Self as EngineTrait >:: BLOCK_SIZE == 0 {
64- self . process_block ( ) ;
65- }
66- inp = & inp[ write_len..] ;
67- }
68- }
69-
70- #[ cfg( feature = "fuzztarget" ) ]
71- fn input ( & mut self , inp : & [ u8 ] ) {
72- for c in inp {
73- self . buffer [ 0 ] ^= * c;
74- }
75- self . length += inp. len ( ) ;
76- }
54+
55+ engine_input_impl ! ( ) ;
7756}
7857
7958/// Output of the SHA1 hash function
Original file line number Diff line number Diff line change @@ -60,30 +60,7 @@ impl EngineTrait for HashEngine {
6060
6161 const BLOCK_SIZE : usize = 64 ;
6262
63- #[ cfg( not( feature = "fuzztarget" ) ) ]
64- fn input ( & mut self , mut inp : & [ u8 ] ) {
65- while !inp. is_empty ( ) {
66- let buf_idx = self . length % <Self as EngineTrait >:: BLOCK_SIZE ;
67- let rem_len = <Self as EngineTrait >:: BLOCK_SIZE - buf_idx;
68- let write_len = cmp:: min ( rem_len, inp. len ( ) ) ;
69-
70- self . buffer [ buf_idx..buf_idx + write_len]
71- . copy_from_slice ( & inp[ ..write_len] ) ;
72- self . length += write_len;
73- if self . length % <Self as EngineTrait >:: BLOCK_SIZE == 0 {
74- self . process_block ( ) ;
75- }
76- inp = & inp[ write_len..] ;
77- }
78- }
79-
80- #[ cfg( feature = "fuzztarget" ) ]
81- fn input ( & mut self , inp : & [ u8 ] ) {
82- for c in inp {
83- self . buffer [ 0 ] ^= * c;
84- }
85- self . length += inp. len ( ) ;
86- }
63+ engine_input_impl ! ( ) ;
8764}
8865
8966/// Output of the SHA256 hash function
Original file line number Diff line number Diff line change @@ -65,29 +65,7 @@ impl EngineTrait for HashEngine {
6565
6666 const BLOCK_SIZE : usize = 128 ;
6767
68- #[ cfg( not( feature = "fuzztarget" ) ) ]
69- fn input ( & mut self , mut inp : & [ u8 ] ) {
70- while !inp. is_empty ( ) {
71- let buf_idx = self . length % <Self as EngineTrait >:: BLOCK_SIZE ;
72- let rem_len = <Self as EngineTrait >:: BLOCK_SIZE - buf_idx;
73- let write_len = cmp:: min ( rem_len, inp. len ( ) ) ;
74-
75- self . buffer [ buf_idx..buf_idx + write_len] . copy_from_slice ( & inp[ ..write_len] ) ;
76- self . length += write_len;
77- if self . length % <Self as EngineTrait >:: BLOCK_SIZE == 0 {
78- self . process_block ( ) ;
79- }
80- inp = & inp[ write_len..] ;
81- }
82- }
83-
84- #[ cfg( feature = "fuzztarget" ) ]
85- fn input ( & mut self , inp : & [ u8 ] ) {
86- for c in inp {
87- self . buffer [ 0 ] ^= * c;
88- }
89- self . length += inp. len ( ) ;
90- }
68+ engine_input_impl ! ( ) ;
9169}
9270
9371/// Output of the SHA256 hash function
Original file line number Diff line number Diff line change @@ -99,6 +99,35 @@ macro_rules! borrow_slice_impl(
9999 )
100100) ;
101101
102+ macro_rules! engine_input_impl(
103+ ( ) => (
104+ #[ cfg( not( feature = "fuzztarget" ) ) ]
105+ fn input( & mut self , mut inp: & [ u8 ] ) {
106+ while !inp. is_empty( ) {
107+ let buf_idx = self . length % <Self as EngineTrait >:: BLOCK_SIZE ;
108+ let rem_len = <Self as EngineTrait >:: BLOCK_SIZE - buf_idx;
109+ let write_len = cmp:: min( rem_len, inp. len( ) ) ;
110+
111+ self . buffer[ buf_idx..buf_idx + write_len]
112+ . copy_from_slice( & inp[ ..write_len] ) ;
113+ self . length += write_len;
114+ if self . length % <Self as EngineTrait >:: BLOCK_SIZE == 0 {
115+ self . process_block( ) ;
116+ }
117+ inp = & inp[ write_len..] ;
118+ }
119+ }
120+
121+ #[ cfg( feature = "fuzztarget" ) ]
122+ fn input( & mut self , inp: & [ u8 ] ) {
123+ for c in inp {
124+ self . buffer[ 0 ] ^= * c;
125+ }
126+ self . length += inp. len( ) ;
127+ }
128+ )
129+ ) ;
130+
102131#[ cfg( test) ]
103132mod test {
104133 use Hash ;
You can’t perform that action at this time.
0 commit comments