diff --git a/include/fc/crypto/elliptic.hpp b/include/fc/crypto/elliptic.hpp index 86334e4..6f37f78 100644 --- a/include/fc/crypto/elliptic.hpp +++ b/include/fc/crypto/elliptic.hpp @@ -5,58 +5,95 @@ #include #include #include +#include -namespace fc { namespace ecc { +namespace fc +{ + namespace ecc + { - namespace detail - { - class public_key_impl; - class private_key_impl; + namespace detail + { + class public_key_impl; + class private_key_impl; + } + + typedef fc::array signature; + typedef fc::array compact_signature; + + class public_key + { + public: + public_key(); + ~public_key(); + bool verify( const fc::sha256& digest, const signature& sig ); + + std::vector serialize()const; + public_key( const std::vector& v ); + public_key( const compact_signature& c, const fc::sha256& digest ); + private: + friend class private_key; + fc::fwd my; + }; + + + class private_key + { + public: + private_key(); + private_key( std::vector k ); + ~private_key(); + + static private_key generate(); + static private_key regenerate( const fc::sha256& secret ); + + fc::sha256 get_secret()const; // get the private key secret + + /** + * Given a public key, calculatse a 512 bit shared secret between that + * key and this private key. + */ + fc::sha512 get_shared_secret( const public_key& pub ); + + signature sign( const fc::sha256& digest ); + compact_signature sign_compact( const fc::sha256& digest ); + bool verify( const fc::sha256& digest, const signature& sig ); + + public_key get_public_key()const; + private: + fc::fwd my; + }; + } // namespace ecc + + namespace raw + { + template + inline void pack( Stream& s, const fc::ecc::public_key& v ) + { + pack( s, v.serialize() ); + } + template + inline void unpack( Stream& s, fc::ecc::public_key& v ) + { + std::vector d; + unpack( s, d ); + v = fc::ecc::public_key( fc::move(d) ); } - typedef fc::array signature; - typedef fc::array compact_signature; - - class public_key + template + inline void pack( Stream& s, const fc::ecc::private_key& v ) { - public: - public_key(); - ~public_key(); - bool verify( const fc::sha256& digest, const signature& sig ); + pack( s, v.get_secret() ); + } - std::vector serialize()const; - public_key( const std::vector& v ); - public_key( const compact_signature& c, const fc::sha256& digest ); - private: - friend class private_key; - fc::fwd my; - }; - - - class private_key + template + inline void unpack( Stream& s, fc::ecc::private_key& v ) { - public: - private_key(); - private_key( std::vector k ); - ~private_key(); + fc::sha256 secret; + unpack( s, secret ); + v = fc::ecc::private_key::regenerate( secret ); + } - static private_key generate(); - static private_key regenerate( const fc::sha256& secret ); + } // namespace raw - fc::sha256 get_secret()const; // get the private key secret - - /** - * Given a public key, calculatse a 512 bit shared secret between that - * key and this private key. - */ - fc::sha512 get_shared_secret( const public_key& pub ); - - signature sign( const fc::sha256& digest ); - compact_signature sign_compact( const fc::sha256& digest ); - bool verify( const fc::sha256& digest, const signature& sig ); - - public_key get_public_key()const; - private: - fc::fwd my; - }; -} } // fc::ecc +} // fc diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index a328c00..5d4b81f 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -127,7 +127,9 @@ namespace fc { template inline void unpack( Stream& s, fc::string& v ) { std::vector tmp; unpack(s,tmp); - v = fc::string(tmp.begin(),tmp.end()); + if( tmp.size() ) + v = fc::string(tmp.data(),tmp.data()+tmp.size()); + else v = fc::string(); } // bool diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index 3cf02e5..ce3159a 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -1,13 +1,23 @@ #pragma once #include #include +#include +#include -namespace fc { namespace raw { +namespace fc { + namespace ecc { class public_key; class private_key; } + namespace raw { template void unpack( Stream& s, fc::optional& v ); template void pack( Stream& s, const fc::optional& v ); template void unpack( Stream& s, fc::string& ); template void pack( Stream& s, const fc::string& ); + template void unpack( Stream& s, std::string& ); + template void pack( Stream& s, const std::string& ); + template void unpack( Stream& s, fc::ecc::public_key& ); + template void pack( Stream& s, const fc::ecc::public_key& ); + template void unpack( Stream& s, fc::ecc::private_key& ); + template void pack( Stream& s, const fc::ecc::private_key& ); template inline void pack( Stream& s, const T& v ); template inline void unpack( Stream& s, T& v );