various updates and enhancements to crypto and network code
This commit is contained in:
parent
154a8ab628
commit
a0f41aa397
6 changed files with 108 additions and 10 deletions
|
|
@ -41,6 +41,8 @@ namespace fc {
|
|||
bigint operator / ( const bigint& a )const;
|
||||
bigint operator % ( const bigint& a )const;
|
||||
bigint operator /= ( const bigint& a );
|
||||
bigint operator *= ( const bigint& a );
|
||||
bigint& operator <<= ( uint32_t i );
|
||||
bigint operator - ( const bigint& a )const;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@
|
|||
#include <fc/exception/exception.hpp>
|
||||
#include <fc/log/logger.hpp>
|
||||
#include <set>
|
||||
#include <unordered_set>
|
||||
|
||||
#define MAX_ARRAY_ALLOC_SIZE (1024*1024*10)
|
||||
|
||||
namespace fc {
|
||||
namespace raw {
|
||||
|
|
@ -21,6 +24,10 @@ namespace fc {
|
|||
inline void pack( Stream& s, const std::set<T>& value );
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, std::set<T>& value );
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const std::unordered_set<T>& value );
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, std::unordered_set<T>& value );
|
||||
|
||||
template<typename Stream>
|
||||
inline void pack( Stream& s, const variant_object& v );
|
||||
|
|
@ -121,6 +128,7 @@ namespace fc {
|
|||
}
|
||||
template<typename Stream> inline void unpack( Stream& s, std::vector<char>& value ) {
|
||||
unsigned_int size; unpack( s, size );
|
||||
FC_ASSERT( size.value < MAX_ARRAY_ALLOC_SIZE );
|
||||
value.resize(size.value);
|
||||
if( value.size() )
|
||||
s.read( value.data(), value.size() );
|
||||
|
|
@ -218,6 +226,29 @@ namespace fc {
|
|||
|
||||
} // namesapce detail
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const std::unordered_set<T>& value ) {
|
||||
pack( s, unsigned_int(value.size()) );
|
||||
auto itr = value.begin();
|
||||
auto end = value.end();
|
||||
while( itr != end ) {
|
||||
fc::raw::pack( s, *itr );
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, std::unordered_set<T>& value ) {
|
||||
unsigned_int size; unpack( s, size );
|
||||
value.clear();
|
||||
FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE );
|
||||
value.reserve(size.value);
|
||||
for( uint32_t i = 0; i < size.value; ++i )
|
||||
{
|
||||
T tmp;
|
||||
fc::raw::unpack( s, tmp );
|
||||
value.insert( std::move(tmp) );
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const std::vector<T>& value ) {
|
||||
|
|
@ -233,6 +264,7 @@ namespace fc {
|
|||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, std::vector<T>& value ) {
|
||||
unsigned_int size; unpack( s, size );
|
||||
FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE );
|
||||
value.resize(size.value);
|
||||
auto itr = value.begin();
|
||||
auto end = value.end();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
#include <fc/string.hpp>
|
||||
#include <fc/crypto/sha1.hpp>
|
||||
#include <fc/io/raw_fwd.hpp>
|
||||
|
||||
namespace fc {
|
||||
|
||||
|
|
@ -15,6 +16,7 @@ namespace fc {
|
|||
operator uint32_t()const;
|
||||
|
||||
friend bool operator==( const address& a, const address& b );
|
||||
friend bool operator!=( const address& a, const address& b );
|
||||
private:
|
||||
uint32_t _ip;
|
||||
};
|
||||
|
|
@ -34,12 +36,8 @@ namespace fc {
|
|||
const address& get_address()const;
|
||||
|
||||
friend bool operator==( const endpoint& a, const endpoint& b );
|
||||
friend bool operator< ( const endpoint& a, const endpoint& b )
|
||||
{
|
||||
return uint32_t(a.get_address()) < uint32_t(b.get_address()) ||
|
||||
(uint32_t(a.get_address()) == uint32_t(b.get_address()) &&
|
||||
uint32_t(a.port()) < uint32_t(b.port()));
|
||||
}
|
||||
friend bool operator!=( const endpoint& a, const endpoint& b );
|
||||
friend bool operator< ( const endpoint& a, const endpoint& b );
|
||||
|
||||
private:
|
||||
/**
|
||||
|
|
@ -59,6 +57,39 @@ namespace fc {
|
|||
|
||||
void to_variant( const ip::address& var, variant& vo );
|
||||
void from_variant( const variant& var, ip::address& vo );
|
||||
|
||||
namespace raw
|
||||
{
|
||||
template<typename Stream>
|
||||
inline void pack( Stream& s, const ip::address& v )
|
||||
{
|
||||
fc::raw::pack( s, uint32_t(v) );
|
||||
}
|
||||
template<typename Stream>
|
||||
inline void unpack( Stream& s, ip::address& v )
|
||||
{
|
||||
uint32_t _ip;
|
||||
fc::raw::unpack( s, _ip );
|
||||
v = ip::address(_ip);
|
||||
}
|
||||
|
||||
template<typename Stream>
|
||||
inline void pack( Stream& s, const ip::endpoint& v )
|
||||
{
|
||||
fc::raw::pack( s, v.get_address() );
|
||||
fc::raw::pack( s, v.port() );
|
||||
}
|
||||
template<typename Stream>
|
||||
inline void unpack( Stream& s, ip::endpoint& v )
|
||||
{
|
||||
ip::address a;
|
||||
uint16_t p;
|
||||
fc::raw::unpack( s, a );
|
||||
fc::raw::unpack( s, p );
|
||||
v = ip::endpoint(a,p);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
namespace std
|
||||
{
|
||||
|
|
|
|||
|
|
@ -118,7 +118,17 @@ namespace fc {
|
|||
BN_CTX_free(ctx);
|
||||
return tmp;
|
||||
}
|
||||
bigint bigint::operator *= ( const bigint& a ) {
|
||||
auto tmp = *this * a;
|
||||
*this = std::move(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bigint& bigint::operator <<= ( uint32_t i )
|
||||
{
|
||||
BN_lshift( n, n, i );
|
||||
return *this;
|
||||
}
|
||||
|
||||
bigint bigint::operator - ( const bigint& a )const {
|
||||
bigint tmp;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,9 @@ namespace fc { namespace ip {
|
|||
bool operator==( const address& a, const address& b ) {
|
||||
return uint32_t(a) == uint32_t(b);
|
||||
}
|
||||
bool operator!=( const address& a, const address& b ) {
|
||||
return uint32_t(a) != uint32_t(b);
|
||||
}
|
||||
|
||||
address& address::operator=( const fc::string& s ) {
|
||||
_ip = boost::asio::ip::address_v4::from_string(s.c_str()).to_ulong();
|
||||
|
|
@ -38,6 +41,17 @@ namespace fc { namespace ip {
|
|||
bool operator==( const endpoint& a, const endpoint& b ) {
|
||||
return a._port == b._port && a._ip == b._ip;
|
||||
}
|
||||
bool operator!=( const endpoint& a, const endpoint& b ) {
|
||||
return a._port != b._port || a._ip != b._ip;
|
||||
}
|
||||
|
||||
bool operator< ( const endpoint& a, const endpoint& b )
|
||||
{
|
||||
return uint32_t(a.get_address()) < uint32_t(b.get_address()) ||
|
||||
(uint32_t(a.get_address()) == uint32_t(b.get_address()) &&
|
||||
uint32_t(a.port()) < uint32_t(b.port()));
|
||||
}
|
||||
|
||||
uint16_t endpoint::port()const { return _port; }
|
||||
const address& endpoint::get_address()const { return _ip; }
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@
|
|||
#include <fc/io/json.hpp>
|
||||
#include <fc/io/stdio.hpp>
|
||||
#include <string.h>
|
||||
#include <fc/crypto/base64.hpp>
|
||||
//#include <fc/crypto/base64.hpp>
|
||||
#include <fc/crypto/hex.hpp>
|
||||
#include <boost/scoped_array.hpp>
|
||||
|
||||
namespace fc
|
||||
|
|
@ -17,13 +18,21 @@ namespace fc
|
|||
void to_variant( const std::vector<char>& var, variant& vo )
|
||||
{
|
||||
if( var.size() )
|
||||
vo = variant(base64_encode((unsigned char*)var.data(),var.size()));
|
||||
//vo = variant(base64_encode((unsigned char*)var.data(),var.size()));
|
||||
vo = variant(to_hex(var.data(),var.size()));
|
||||
else vo = "";
|
||||
}
|
||||
void from_variant( const variant& var, std::vector<char>& vo )
|
||||
{
|
||||
std::string b64 = base64_decode( var.as_string() );
|
||||
vo = std::vector<char>( b64.c_str(), b64.c_str() + b64.size() );
|
||||
auto str = var.as_string();
|
||||
vo.resize( str.size() / 2 );
|
||||
if( vo.size() )
|
||||
{
|
||||
size_t r = from_hex( str, vo.data(), vo.size() );
|
||||
FC_ASSERT( r = vo.size() );
|
||||
}
|
||||
// std::string b64 = base64_decode( var.as_string() );
|
||||
// vo = std::vector<char>( b64.c_str(), b64.c_str() + b64.size() );
|
||||
}
|
||||
/**
|
||||
* The TypeID is stored in the 'last byte' of the variant.
|
||||
|
|
|
|||
Loading…
Reference in a new issue