@@ -12,7 +12,7 @@ use clap::Parser;
1212use colored:: * ;
1313use hex:: encode;
1414use sha1:: Sha1 ;
15- use sha2:: { Digest , Sha256 } ;
15+ use sha2:: { Digest , Sha256 , Sha512 } ;
1616
1717use crate :: cli:: { ChecksumType , Cli , Command } ;
1818
@@ -75,6 +75,16 @@ fn process_checksum(
7575 bail ! ( "Error opening file" ) ;
7676 }
7777 }
78+ ChecksumType :: Sha512 => {
79+ let bytes = fs:: read ( file_path) ;
80+ if let Ok ( b) = bytes {
81+ let mut hasher = Sha512 :: new ( ) ;
82+ hasher. update ( b) ;
83+ encode ( hasher. finalize ( ) )
84+ } else {
85+ bail ! ( "Error opening file" ) ;
86+ }
87+ }
7888 ChecksumType :: Sha1 => {
7989 let bytes = fs:: read ( file_path) ;
8090 if let Ok ( b) = bytes {
@@ -235,6 +245,12 @@ mod tests {
235245 hasher. update ( bytes) ;
236246 encode ( hasher. finalize ( ) )
237247 }
248+ ChecksumType :: Sha512 => {
249+ let bytes = fs:: read ( file) . unwrap ( ) ;
250+ let mut hasher = Sha512 :: new ( ) ;
251+ hasher. update ( bytes) ;
252+ encode ( hasher. finalize ( ) )
253+ }
238254 ChecksumType :: Sha1 => {
239255 let bytes = fs:: read ( file) . unwrap ( ) ;
240256 let mut hasher = Sha1 :: new ( ) ;
@@ -474,6 +490,108 @@ mod tests {
474490 assert ! ( result. contains( & checksum_2) ) ;
475491 }
476492
493+ #[ test]
494+ fn generate_sha512 ( ) {
495+ let test_file = fake_file_path ( ) ;
496+ let base = tempdir ( ) . unwrap ( ) . path ( ) . to_path_buf ( ) ;
497+ fs:: create_dir_all ( & base) . unwrap ( ) ;
498+ let output_file = base. join ( "output.txt" ) ;
499+
500+ process_checksum (
501+ & test_file,
502+ & Some ( output_file. clone ( ) ) ,
503+ & ChecksumType :: Sha512 ,
504+ false ,
505+ false ,
506+ )
507+ . unwrap ( ) ;
508+
509+ assert ! ( output_file. exists( ) ) ;
510+
511+ let checksum = get_checksum ( & test_file, ChecksumType :: Sha512 ) ;
512+ let result = fs:: read_to_string ( & output_file) . unwrap ( ) ;
513+
514+ assert ! ( result. contains( & checksum) ) ;
515+ }
516+
517+ #[ test]
518+ fn generate_sha512_file_directory_overwrite ( ) {
519+ let test_file_1 = fake_file_path ( ) ;
520+ let test_file_2 = fake_file_path_2 ( ) ;
521+ let base = tempdir ( ) . unwrap ( ) . path ( ) . to_path_buf ( ) ;
522+ fs:: create_dir_all ( & base) . unwrap ( ) ;
523+ let output_file = base. join ( "output.txt" ) ;
524+
525+ process_checksum (
526+ & test_file_1,
527+ & Some ( output_file. clone ( ) ) ,
528+ & ChecksumType :: Sha512 ,
529+ true ,
530+ false ,
531+ )
532+ . unwrap ( ) ;
533+
534+ assert ! ( output_file. exists( ) ) ;
535+
536+ let checksum_1 = get_checksum ( & test_file_1, ChecksumType :: Sha512 ) ;
537+ let result_1 = fs:: read_to_string ( & output_file) . unwrap ( ) ;
538+
539+ assert ! ( result_1. contains( & checksum_1) ) ;
540+
541+ process_checksum (
542+ & test_file_2,
543+ & Some ( output_file. clone ( ) ) ,
544+ & ChecksumType :: Sha512 ,
545+ true ,
546+ false ,
547+ )
548+ . unwrap ( ) ;
549+
550+ assert ! ( output_file. exists( ) ) ;
551+
552+ let checksum_2 = get_checksum ( & test_file_2, ChecksumType :: Sha512 ) ;
553+ let result_2 = fs:: read_to_string ( & output_file) . unwrap ( ) ;
554+
555+ assert ! ( !result_2. contains( & checksum_1) ) ;
556+ assert ! ( result_2. contains( & checksum_2) ) ;
557+ }
558+
559+ #[ test]
560+ fn generate_sha512_file_directory_no_overwrite ( ) {
561+ let test_file_1 = fake_file_path ( ) ;
562+ let test_file_2 = fake_file_path_2 ( ) ;
563+ let base = tempdir ( ) . unwrap ( ) . path ( ) . to_path_buf ( ) ;
564+ fs:: create_dir_all ( & base) . unwrap ( ) ;
565+ let output_file = base. join ( "output.txt" ) ;
566+
567+ process_checksum (
568+ & test_file_1,
569+ & Some ( output_file. clone ( ) ) ,
570+ & ChecksumType :: Sha512 ,
571+ false ,
572+ false ,
573+ )
574+ . unwrap ( ) ;
575+
576+ process_checksum (
577+ & test_file_2,
578+ & Some ( output_file. clone ( ) ) ,
579+ & ChecksumType :: Sha512 ,
580+ false ,
581+ false ,
582+ )
583+ . unwrap ( ) ;
584+
585+ assert ! ( output_file. exists( ) ) ;
586+
587+ let checksum_1 = get_checksum ( & test_file_1, ChecksumType :: Sha512 ) ;
588+ let checksum_2 = get_checksum ( & test_file_2, ChecksumType :: Sha512 ) ;
589+ let result = fs:: read_to_string ( & output_file) . unwrap ( ) ;
590+
591+ assert ! ( result. contains( & checksum_1) ) ;
592+ assert ! ( result. contains( & checksum_2) ) ;
593+ }
594+
477595 #[ test]
478596 fn generate_sha1_file ( ) {
479597 let test_file = fake_file_path ( ) ;
0 commit comments