Const correctness and const-ref in Crypto and elsewhere.

This commit is contained in:
John Hood
2015-12-25 22:18:04 -05:00
parent 13928e9c10
commit 6abd4739de
16 changed files with 46 additions and 46 deletions
+10 -10
View File
@@ -184,14 +184,14 @@ Nonce::Nonce( uint64_t val )
memcpy( bytes + 4, &val_net, 8 );
}
uint64_t Nonce::val( void )
uint64_t Nonce::val( void ) const
{
uint64_t ret;
memcpy( &ret, bytes + 4, 8 );
return be64toh( ret );
}
Nonce::Nonce( char *s_bytes, size_t len )
Nonce::Nonce( const char *s_bytes, size_t len )
{
if ( len != 8 ) {
throw CryptoException( "Nonce representation must be 8 octets long." );
@@ -201,18 +201,18 @@ Nonce::Nonce( char *s_bytes, size_t len )
memcpy( bytes + 4, s_bytes, 8 );
}
Message::Message( char *nonce_bytes, size_t nonce_len,
char *text_bytes, size_t text_len )
Message::Message( const char *nonce_bytes, size_t nonce_len,
const char *text_bytes, size_t text_len )
: nonce( nonce_bytes, nonce_len ),
text( (char *)text_bytes, text_len )
text( text_bytes, text_len )
{}
Message::Message( Nonce s_nonce, string s_text )
Message::Message( const Nonce & s_nonce, const string & s_text )
: nonce( s_nonce ),
text( s_text )
{}
string Session::encrypt( Message plaintext )
const string Session::encrypt( const Message & plaintext )
{
const size_t pt_len = plaintext.text.size();
const int ciphertext_len = pt_len + 16;
@@ -262,13 +262,13 @@ string Session::encrypt( Message plaintext )
return plaintext.nonce.cc_str() + text;
}
Message Session::decrypt( string ciphertext )
const Message Session::decrypt( const string & ciphertext )
{
if ( ciphertext.size() < 24 ) {
throw CryptoException( "Ciphertext must contain nonce and tag." );
}
char *str = (char *)ciphertext.data();
const char *str = ciphertext.data();
int body_len = ciphertext.size() - 8;
int pt_len = body_len - 16;
@@ -297,7 +297,7 @@ Message Session::decrypt( string ciphertext )
throw CryptoException( "Packet failed integrity check." );
}
Message ret( nonce, string( plaintext_buffer.data(), pt_len ) );
const Message ret( nonce, string( plaintext_buffer.data(), pt_len ) );
return ret;
}
+9 -9
View File
@@ -109,21 +109,21 @@ namespace Crypto {
public:
Nonce( uint64_t val );
Nonce( char *s_bytes, size_t len );
Nonce( const char *s_bytes, size_t len );
string cc_str( void ) const { return string( bytes + 4, 8 ); }
const char *data( void ) const { return bytes; }
uint64_t val( void );
uint64_t val( void ) const;
};
class Message {
public:
Nonce nonce;
string text;
const Nonce nonce;
const string text;
Message( char *nonce_bytes, size_t nonce_len,
char *text_bytes, size_t text_len );
Message( Nonce s_nonce, string s_text );
Message( const char *nonce_bytes, size_t nonce_len,
const char *text_bytes, size_t text_len );
Message( const Nonce & s_nonce, const string & s_text );
};
class Session {
@@ -145,8 +145,8 @@ namespace Crypto {
Session( Base64Key s_key );
~Session();
string encrypt( Message plaintext );
Message decrypt( string ciphertext );
const string encrypt( const Message & plaintext );
const Message decrypt( const string & ciphertext );
Session( const Session & );
Session & operator=( const Session & );