diff --git a/src/examples/decrypt.cc b/src/examples/decrypt.cc index eb4e9fc..530d3fa 100644 --- a/src/examples/decrypt.cc +++ b/src/examples/decrypt.cc @@ -31,12 +31,8 @@ */ #include -#include -#include -#include -#include -#include #include +#include #include "crypto.h" @@ -55,31 +51,12 @@ int main( int argc, char *argv[] ) Session session( key ); /* Read input */ - char *input = NULL; - int total_size = 0; - - while ( 1 ) { - unsigned char buf[ 16384 ]; - ssize_t bytes_read = read( STDIN_FILENO, buf, 16384 ); - if ( bytes_read == 0 ) { /* EOF */ - break; - } else if ( bytes_read < 0 ) { - perror( "read" ); - exit( 1 ); - } else { - input = (char *)realloc( input, total_size + bytes_read ); - assert( input ); - memcpy( input + total_size, buf, bytes_read ); - total_size += bytes_read; - } - } - - string ciphertext( input, total_size ); - free( input ); + ostringstream input; + input << cin.rdbuf(); /* Decrypt message */ - Message message = session.decrypt( ciphertext ); + Message message = session.decrypt( input.str() ); fprintf( stderr, "Nonce = %ld\n", (long)message.nonce.val() ); diff --git a/src/examples/encrypt.cc b/src/examples/encrypt.cc index 3bf0d64..b5c349d 100644 --- a/src/examples/encrypt.cc +++ b/src/examples/encrypt.cc @@ -31,12 +31,8 @@ */ #include -#include -#include -#include -#include -#include #include +#include #include "crypto.h" @@ -56,31 +52,12 @@ int main( int argc, char *argv[] ) Nonce nonce( myatoi( argv[ 1 ] ) ); /* Read input */ - char *input = NULL; - int total_size = 0; - - while ( 1 ) { - unsigned char buf[ 16384 ]; - ssize_t bytes_read = read( STDIN_FILENO, buf, 16384 ); - if ( bytes_read == 0 ) { /* EOF */ - break; - } else if ( bytes_read < 0 ) { - perror( "read" ); - exit( 1 ); - } else { - input = (char *)realloc( input, total_size + bytes_read ); - assert( input ); - memcpy( input + total_size, buf, bytes_read ); - total_size += bytes_read; - } - } - - string plaintext( input, total_size ); - free( input ); + ostringstream input; + input << cin.rdbuf(); /* Encrypt message */ - string ciphertext = session.encrypt( Message( nonce, plaintext ) ); + string ciphertext = session.encrypt( Message( nonce, input.str() ) ); cerr << "Key: " << key.printable_key() << endl;