src/examples/{encrypt,decrypt}: Read input using C++
cppcheck found an exception safety bug in the old code, but like, really now. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
committed by
Keith Winstein
parent
ecdd2dd648
commit
5f807dfdf8
+4
-27
@@ -31,12 +31,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
|
||||||
@@ -55,31 +51,12 @@ int main( int argc, char *argv[] )
|
|||||||
Session session( key );
|
Session session( key );
|
||||||
|
|
||||||
/* Read input */
|
/* Read input */
|
||||||
char *input = NULL;
|
ostringstream input;
|
||||||
int total_size = 0;
|
input << cin.rdbuf();
|
||||||
|
|
||||||
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 );
|
|
||||||
|
|
||||||
/* Decrypt message */
|
/* Decrypt message */
|
||||||
|
|
||||||
Message message = session.decrypt( ciphertext );
|
Message message = session.decrypt( input.str() );
|
||||||
|
|
||||||
fprintf( stderr, "Nonce = %ld\n",
|
fprintf( stderr, "Nonce = %ld\n",
|
||||||
(long)message.nonce.val() );
|
(long)message.nonce.val() );
|
||||||
|
|||||||
+4
-27
@@ -31,12 +31,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
|
||||||
@@ -56,31 +52,12 @@ int main( int argc, char *argv[] )
|
|||||||
Nonce nonce( myatoi( argv[ 1 ] ) );
|
Nonce nonce( myatoi( argv[ 1 ] ) );
|
||||||
|
|
||||||
/* Read input */
|
/* Read input */
|
||||||
char *input = NULL;
|
ostringstream input;
|
||||||
int total_size = 0;
|
input << cin.rdbuf();
|
||||||
|
|
||||||
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 );
|
|
||||||
|
|
||||||
/* Encrypt message */
|
/* Encrypt message */
|
||||||
|
|
||||||
string ciphertext = session.encrypt( Message( nonce, plaintext ) );
|
string ciphertext = session.encrypt( Message( nonce, input.str() ) );
|
||||||
|
|
||||||
cerr << "Key: " << key.printable_key() << endl;
|
cerr << "Key: " << key.printable_key() << endl;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user