@@ -690,8 +690,8 @@ static int parse_plumbing_options (const char** key_name, const char** key_file,
690
690
return parse_options (options, argc, argv);
691
691
}
692
692
693
- // Encrypt contents of stdin and write to stdout
694
- int clean (int argc, const char ** argv)
693
+ // Encrypt contents of &in and write to &out
694
+ int clean (int argc, const char ** argv, std::istream& in, std::ostream& out )
695
695
{
696
696
const char * key_name = 0 ;
697
697
const char * key_path = 0 ;
@@ -724,10 +724,10 @@ int clean (int argc, const char** argv)
724
724
725
725
char buffer[1024 ];
726
726
727
- while (std::cin && file_size < Aes_ctr_encryptor::MAX_CRYPT_BYTES) {
728
- std::cin .read (buffer, sizeof (buffer));
727
+ while (in && file_size < Aes_ctr_encryptor::MAX_CRYPT_BYTES) {
728
+ in .read (buffer, sizeof (buffer));
729
729
730
- const size_t bytes_read = std::cin .gcount ();
730
+ const size_t bytes_read = in .gcount ();
731
731
732
732
hmac.add (reinterpret_cast <unsigned char *>(buffer), bytes_read);
733
733
file_size += bytes_read;
@@ -775,8 +775,8 @@ int clean (int argc, const char** argv)
775
775
hmac.get (digest);
776
776
777
777
// Write a header that...
778
- std::cout .write (" \0 GITCRYPT\0 " , 10 ); // ...identifies this as an encrypted file
779
- std::cout .write (reinterpret_cast <char *>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce
778
+ out .write (" \0 GITCRYPT\0 " , 10 ); // ...identifies this as an encrypted file
779
+ out .write (reinterpret_cast <char *>(digest), Aes_ctr_encryptor::NONCE_LEN); // ...includes the nonce
780
780
781
781
// Now encrypt the file and write to stdout
782
782
Aes_ctr_encryptor aes (key->aes_key , digest);
@@ -787,7 +787,7 @@ int clean (int argc, const char** argv)
787
787
while (file_data_len > 0 ) {
788
788
const size_t buffer_len = std::min (sizeof (buffer), file_data_len);
789
789
aes.process (file_data, reinterpret_cast <unsigned char *>(buffer), buffer_len);
790
- std::cout .write (buffer, buffer_len);
790
+ out .write (buffer, buffer_len);
791
791
file_data += buffer_len;
792
792
file_data_len -= buffer_len;
793
793
}
@@ -803,14 +803,14 @@ int clean (int argc, const char** argv)
803
803
aes.process (reinterpret_cast <unsigned char *>(buffer),
804
804
reinterpret_cast <unsigned char *>(buffer),
805
805
buffer_len);
806
- std::cout .write (buffer, buffer_len);
806
+ out .write (buffer, buffer_len);
807
807
}
808
808
}
809
809
810
810
return 0 ;
811
811
}
812
812
813
- static int decrypt_file_to_stdout (const Key_file& key_file, const unsigned char * header, std::istream& in)
813
+ static int decrypt_file_to_stream (const Key_file& key_file, const unsigned char * header, std::istream& in, std::ostream& out = std::cout )
814
814
{
815
815
const unsigned char * nonce = header + 10 ;
816
816
uint32_t key_version = 0 ; // TODO: get the version from the file header
@@ -828,7 +828,7 @@ static int decrypt_file_to_stdout (const Key_file& key_file, const unsigned char
828
828
in.read (reinterpret_cast <char *>(buffer), sizeof (buffer));
829
829
aes.process (buffer, buffer, in.gcount ());
830
830
hmac.add (buffer, in.gcount ());
831
- std::cout .write (reinterpret_cast <char *>(buffer), in.gcount ());
831
+ out .write (reinterpret_cast <char *>(buffer), in.gcount ());
832
832
}
833
833
834
834
unsigned char digest[Hmac_sha1_state::LEN];
@@ -844,8 +844,8 @@ static int decrypt_file_to_stdout (const Key_file& key_file, const unsigned char
844
844
return 0 ;
845
845
}
846
846
847
- // Decrypt contents of stdin and write to stdout
848
- int smudge (int argc, const char ** argv)
847
+ // Decrypt contents of &in and write to &out
848
+ int smudge (int argc, const char ** argv, std::istream& in, std::ostream& out )
849
849
{
850
850
const char * key_name = 0 ;
851
851
const char * key_path = 0 ;
@@ -864,21 +864,21 @@ int smudge (int argc, const char** argv)
864
864
865
865
// Read the header to get the nonce and make sure it's actually encrypted
866
866
unsigned char header[10 + Aes_ctr_decryptor::NONCE_LEN];
867
- std::cin .read (reinterpret_cast <char *>(header), sizeof (header));
868
- if (std::cin .gcount () != sizeof (header) || std::memcmp (header, " \0 GITCRYPT\0 " , 10 ) != 0 ) {
867
+ in .read (reinterpret_cast <char *>(header), sizeof (header));
868
+ if (in .gcount () != sizeof (header) || std::memcmp (header, " \0 GITCRYPT\0 " , 10 ) != 0 ) {
869
869
// File not encrypted - just copy it out to stdout
870
870
std::clog << " git-crypt: Warning: file not encrypted" << std::endl;
871
871
std::clog << " git-crypt: Run 'git-crypt status' to make sure all files are properly encrypted." << std::endl;
872
872
std::clog << " git-crypt: If 'git-crypt status' reports no problems, then an older version of" << std::endl;
873
873
std::clog << " git-crypt: this file may be unencrypted in the repository's history. If this" << std::endl;
874
874
std::clog << " git-crypt: file contains sensitive information, you can use 'git filter-branch'" << std::endl;
875
875
std::clog << " git-crypt: to remove its old versions from the history." << std::endl;
876
- std::cout .write (reinterpret_cast <char *>(header), std::cin .gcount ()); // include the bytes which we already read
877
- std::cout << std::cin .rdbuf ();
876
+ out .write (reinterpret_cast <char *>(header), in .gcount ()); // include the bytes which we already read
877
+ out << in .rdbuf ();
878
878
return 0 ;
879
879
}
880
880
881
- return decrypt_file_to_stdout (key_file, header, std::cin );
881
+ return decrypt_file_to_stream (key_file, header, in, out );
882
882
}
883
883
884
884
int diff (int argc, const char ** argv)
@@ -920,7 +920,7 @@ int diff (int argc, const char** argv)
920
920
}
921
921
922
922
// Go ahead and decrypt it
923
- return decrypt_file_to_stdout (key_file, header, in);
923
+ return decrypt_file_to_stream (key_file, header, in);
924
924
}
925
925
926
926
void help_init (std::ostream& out)
0 commit comments