adding aes_load and aes_save methods

This commit is contained in:
Daniel Larimer 2013-08-31 13:12:55 -04:00
parent 8fc6f79dc9
commit 5bf5cf9219
3 changed files with 57 additions and 1 deletions

View file

@ -6,6 +6,7 @@
#include <vector>
namespace fc {
class path;
class aes_encoder
{
@ -27,4 +28,14 @@ namespace fc {
std::vector<char> aes_encrypt( const fc::sha512& key, const std::vector<char>& plain_text );
std::vector<char> aes_decrypt( const fc::sha512& key, const std::vector<char>& cipher_text );
}
/** encrypts plain_text and then includes a checksum that enables us to verify the integrety of
* the file / key prior to decryption.
*/
void aes_save( const fc::path& file, const fc::sha512& key, std::vector<char> plain_text );
/**
* recovers the plain_text saved via aes_save()
*/
std::vector<char> aes_load( const fc::path& file, const fc::sha512& key );
} // namespace fc

View file

@ -72,6 +72,8 @@ class ripemd160
void to_variant( const ripemd160& bi, variant& v );
void from_variant( const variant& v, ripemd160& bi );
typedef ripemd160 uint160_t;
} // namespace fc
namespace std

View file

@ -3,6 +3,9 @@
#include <fc/exception/exception.hpp>
#include <fc/fwd_impl.hpp>
#include <fc/io/fstream.hpp>
#include <fc/io/raw.hpp>
namespace fc {
static int init = init_openssl();
@ -188,4 +191,44 @@ std::vector<char> aes_decrypt( const fc::sha512& key, const std::vector<char>& c
return plain_text;
}
/** encrypts plain_text and then includes a checksum that enables us to verify the integrety of
* the file / key prior to decryption.
*/
void aes_save( const fc::path& file, const fc::sha512& key, std::vector<char> plain_text )
{ try {
auto cipher = aes_encrypt( key, plain_text );
fc::sha512::encoder check_enc;
fc::raw::pack( check_enc, key );
fc::raw::pack( check_enc, cipher );
auto check = check_enc.result();
fc::ofstream out(file);
fc::raw::pack( out, check );
fc::raw::pack( out, cipher );
} FC_RETHROW_EXCEPTIONS( warn, "", ("file",file) ) }
/**
* recovers the plain_text saved via aes_save()
*/
std::vector<char> aes_load( const fc::path& file, const fc::sha512& key )
{ try {
FC_ASSERT( fc::exists( file ) );
fc::ifstream in( file, fc::ifstream::binary );
fc::sha512 check;
std::vector<char> cipher;
fc::raw::unpack( in, check );
fc::raw::unpack( in, cipher );
fc::sha512::encoder check_enc;
fc::raw::pack( check_enc, key );
fc::raw::pack( check_enc, cipher );
FC_ASSERT( check_enc.result() == check );
return aes_decrypt( key, cipher );
} FC_RETHROW_EXCEPTIONS( warn, "", ("file",file) ) }
} // namespace fc