@@ -97,6 +97,12 @@ impl Blake2b {
9797 }
9898}
9999
100+ impl Default for Blake2b {
101+ fn default ( ) -> Self {
102+ Self :: with_output_bytes ( Self :: DEFAULT_BYTE_SIZE )
103+ }
104+ }
105+
100106impl Digest for Blake2b {
101107 fn hash_update ( & mut self , input : & [ u8 ] ) {
102108 self . digest . update ( input) ;
@@ -116,14 +122,9 @@ impl Digest for Blake2b {
116122 }
117123}
118124
125+ #[ derive( Default ) ]
119126pub struct Blake3 ( blake3:: Hasher ) ;
120127
121- impl Blake3 {
122- pub fn new ( ) -> Self {
123- Self ( blake3:: Hasher :: new ( ) )
124- }
125- }
126-
127128impl Digest for Blake3 {
128129 fn hash_update ( & mut self , input : & [ u8 ] ) {
129130 self . 0 . update ( input) ;
@@ -135,22 +136,17 @@ impl Digest for Blake3 {
135136 }
136137
137138 fn reset ( & mut self ) {
138- * self = Self :: new ( ) ;
139+ * self = Self :: default ( ) ;
139140 }
140141
141142 fn output_bits ( & self ) -> usize {
142143 256
143144 }
144145}
145146
147+ #[ derive( Default ) ]
146148pub struct Sm3 ( sm3:: Sm3 ) ;
147149
148- impl Sm3 {
149- pub fn new ( ) -> Self {
150- Self ( <sm3:: Sm3 as sm3:: Digest >:: new ( ) )
151- }
152- }
153-
154150impl Digest for Sm3 {
155151 fn hash_update ( & mut self , input : & [ u8 ] ) {
156152 <sm3:: Sm3 as sm3:: Digest >:: update ( & mut self . 0 , input) ;
@@ -161,7 +157,7 @@ impl Digest for Sm3 {
161157 }
162158
163159 fn reset ( & mut self ) {
164- * self = Self :: new ( ) ;
160+ * self = Self :: default ( ) ;
165161 }
166162
167163 fn output_bits ( & self ) -> usize {
@@ -188,8 +184,10 @@ impl Crc {
188184 0 , // Check value (not used)
189185 )
190186 }
187+ }
191188
192- pub fn new ( ) -> Self {
189+ impl Default for Crc {
190+ fn default ( ) -> Self {
193191 Self {
194192 digest : crc_fast:: Digest :: new_with_params ( Self :: get_posix_cksum_params ( ) ) ,
195193 size : 0 ,
@@ -236,8 +234,8 @@ pub struct CRC32B {
236234 digest : crc_fast:: Digest ,
237235}
238236
239- impl CRC32B {
240- pub fn new ( ) -> Self {
237+ impl Default for CRC32B {
238+ fn default ( ) -> Self {
241239 Self {
242240 digest : crc_fast:: Digest :: new ( crc_fast:: CrcAlgorithm :: Crc32IsoHdlc ) ,
243241 }
@@ -272,16 +270,11 @@ impl Digest for CRC32B {
272270 }
273271}
274272
273+ #[ derive( Default ) ]
275274pub struct Bsd {
276275 state : u16 ,
277276}
278277
279- impl Bsd {
280- pub fn new ( ) -> Self {
281- Self { state : 0 }
282- }
283- }
284-
285278impl Digest for Bsd {
286279 fn hash_update ( & mut self , input : & [ u8 ] ) {
287280 for & byte in input {
@@ -301,24 +294,19 @@ impl Digest for Bsd {
301294 }
302295
303296 fn reset ( & mut self ) {
304- * self = Self :: new ( ) ;
297+ * self = Self :: default ( ) ;
305298 }
306299
307300 fn output_bits ( & self ) -> usize {
308301 128
309302 }
310303}
311304
305+ #[ derive( Default ) ]
312306pub struct SysV {
313307 state : u32 ,
314308}
315309
316- impl SysV {
317- pub fn new ( ) -> Self {
318- Self { state : 0 }
319- }
320- }
321-
322310impl Digest for SysV {
323311 fn hash_update ( & mut self , input : & [ u8 ] ) {
324312 for & byte in input {
@@ -339,7 +327,7 @@ impl Digest for SysV {
339327 }
340328
341329 fn reset ( & mut self ) {
342- * self = Self :: new ( ) ;
330+ * self = Self :: default ( ) ;
343331 }
344332
345333 fn output_bits ( & self ) -> usize {
@@ -350,8 +338,8 @@ impl Digest for SysV {
350338// Implements the Digest trait for sha2 / sha3 algorithms with fixed output
351339macro_rules! impl_digest_common {
352340 ( $algo_type: ty, $size: literal) => {
353- impl $algo_type {
354- pub fn new ( ) -> Self {
341+ impl Default for $algo_type {
342+ fn default ( ) -> Self {
355343 Self ( Default :: default ( ) )
356344 }
357345 }
@@ -365,7 +353,7 @@ macro_rules! impl_digest_common {
365353 }
366354
367355 fn reset( & mut self ) {
368- * self = Self :: new ( ) ;
356+ * self = Self :: default ( ) ;
369357 }
370358
371359 fn output_bits( & self ) -> usize {
@@ -388,6 +376,11 @@ macro_rules! impl_digest_shake {
388376 }
389377 }
390378 }
379+ impl Default for $algo_type {
380+ fn default ( ) -> Self {
381+ Self :: with_output_bits( Self :: DEFAULT_BIT_SIZE )
382+ }
383+ }
391384 impl Digest for $algo_type {
392385 fn hash_update( & mut self , input: & [ u8 ] ) {
393386 digest:: Update :: update( & mut self . digest, input) ;
@@ -600,8 +593,8 @@ mod tests {
600593 #[ test]
601594 fn test_crc_basic_functionality ( ) {
602595 // Test that our CRC implementation works with basic functionality
603- let mut crc1 = Crc :: new ( ) ;
604- let mut crc2 = Crc :: new ( ) ;
596+ let mut crc1 = Crc :: default ( ) ;
597+ let mut crc2 = Crc :: default ( ) ;
605598
606599 // Same input should give same output
607600 crc1. hash_update ( b"test" ) ;
@@ -617,15 +610,15 @@ mod tests {
617610
618611 #[ test]
619612 fn test_crc_digest_basic ( ) {
620- let mut crc = Crc :: new ( ) ;
613+ let mut crc = Crc :: default ( ) ;
621614
622615 // Test empty input
623616 let mut output = [ 0u8 ; 8 ] ;
624617 crc. hash_finalize ( & mut output) ;
625618 let empty_result = u64:: from_ne_bytes ( output) ;
626619
627620 // Reset and test with "test" string
628- let mut crc = Crc :: new ( ) ;
621+ let mut crc = Crc :: default ( ) ;
629622 crc. hash_update ( b"test" ) ;
630623 crc. hash_finalize ( & mut output) ;
631624 let test_result = u64:: from_ne_bytes ( output) ;
@@ -639,8 +632,8 @@ mod tests {
639632
640633 #[ test]
641634 fn test_crc_digest_incremental ( ) {
642- let mut crc1 = Crc :: new ( ) ;
643- let mut crc2 = Crc :: new ( ) ;
635+ let mut crc1 = Crc :: default ( ) ;
636+ let mut crc2 = Crc :: default ( ) ;
644637
645638 // Test that processing in chunks gives same result as all at once
646639 let data = b"Hello, World! This is a test string for CRC computation." ;
@@ -665,13 +658,13 @@ mod tests {
665658 // Test that our optimized slice-by-8 gives same results as byte-by-byte
666659 let test_data = b"This is a longer test string to verify slice-by-8 optimization works correctly with various data sizes including remainders." ;
667660
668- let mut crc_optimized = Crc :: new ( ) ;
661+ let mut crc_optimized = Crc :: default ( ) ;
669662 crc_optimized. hash_update ( test_data) ;
670663 let mut output_opt = [ 0u8 ; 8 ] ;
671664 crc_optimized. hash_finalize ( & mut output_opt) ;
672665
673666 // Create a reference implementation using hash_update
674- let mut crc_reference = Crc :: new ( ) ;
667+ let mut crc_reference = Crc :: default ( ) ;
675668 for & byte in test_data {
676669 crc_reference. hash_update ( & [ byte] ) ;
677670 }
@@ -692,7 +685,7 @@ mod tests {
692685 ] ;
693686
694687 for ( input, expected) in test_cases {
695- let mut crc = Crc :: new ( ) ;
688+ let mut crc = Crc :: default ( ) ;
696689 crc. hash_update ( input. as_bytes ( ) ) ;
697690 let mut output = [ 0u8 ; 8 ] ;
698691 crc. hash_finalize ( & mut output) ;
@@ -704,14 +697,14 @@ mod tests {
704697
705698 #[ test]
706699 fn test_crc_hash_update_edge_cases ( ) {
707- let mut crc = Crc :: new ( ) ;
700+ let mut crc = Crc :: default ( ) ;
708701
709702 // Test with data that's not a multiple of 8 bytes
710703 let data7 = b"1234567" ; // 7 bytes
711704 crc. hash_update ( data7) ;
712705
713706 let data9 = b"123456789" ; // 9 bytes
714- let mut crc2 = Crc :: new ( ) ;
707+ let mut crc2 = Crc :: default ( ) ;
715708 crc2. hash_update ( data9) ;
716709
717710 // Should not panic and should produce valid results
0 commit comments