adding ripemd160
This commit is contained in:
parent
c8839553c0
commit
8a9d0c9e13
3 changed files with 205 additions and 0 deletions
|
|
@ -99,6 +99,7 @@ set( fc_sources
|
||||||
src/crypto/bigint.cpp
|
src/crypto/bigint.cpp
|
||||||
src/crypto/hex.cpp
|
src/crypto/hex.cpp
|
||||||
src/crypto/sha1.cpp
|
src/crypto/sha1.cpp
|
||||||
|
src/crypto/ripemd160.cpp
|
||||||
src/crypto/sha256.cpp
|
src/crypto/sha256.cpp
|
||||||
src/crypto/sha224.cpp
|
src/crypto/sha224.cpp
|
||||||
src/crypto/sha512.cpp
|
src/crypto/sha512.cpp
|
||||||
|
|
|
||||||
89
include/fc/crypto/ripemd160.hpp
Normal file
89
include/fc/crypto/ripemd160.hpp
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
#pragma once
|
||||||
|
#include <fc/fwd.hpp>
|
||||||
|
#include <fc/string.hpp>
|
||||||
|
|
||||||
|
namespace fc{
|
||||||
|
class sha512;
|
||||||
|
class sha256;
|
||||||
|
|
||||||
|
class ripemd160
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ripemd160();
|
||||||
|
explicit ripemd160( const string& hex_str );
|
||||||
|
|
||||||
|
string str()const;
|
||||||
|
operator string()const;
|
||||||
|
|
||||||
|
char* data()const;
|
||||||
|
|
||||||
|
static ripemd160 hash( const fc::sha512& h );
|
||||||
|
static ripemd160 hash( const fc::sha256& h );
|
||||||
|
static ripemd160 hash( const char* d, uint32_t dlen );
|
||||||
|
static ripemd160 hash( const string& );
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static ripemd160 hash( const T& t )
|
||||||
|
{
|
||||||
|
ripemd160::encoder e;
|
||||||
|
e << t;
|
||||||
|
return e.result();
|
||||||
|
}
|
||||||
|
|
||||||
|
class encoder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
encoder();
|
||||||
|
~encoder();
|
||||||
|
|
||||||
|
void write( const char* d, uint32_t dlen );
|
||||||
|
void put( char c ) { write( &c, 1 ); }
|
||||||
|
void reset();
|
||||||
|
ripemd160 result();
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct impl;
|
||||||
|
fc::fwd<impl,96> my;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline friend T& operator<<( T& ds, const ripemd160& ep ) {
|
||||||
|
ds.write( ep.data(), sizeof(ep) );
|
||||||
|
return ds;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline friend T& operator>>( T& ds, ripemd160& ep ) {
|
||||||
|
ds.read( ep.data(), sizeof(ep) );
|
||||||
|
return ds;
|
||||||
|
}
|
||||||
|
friend ripemd160 operator << ( const ripemd160& h1, uint32_t i );
|
||||||
|
friend bool operator == ( const ripemd160& h1, const ripemd160& h2 );
|
||||||
|
friend bool operator != ( const ripemd160& h1, const ripemd160& h2 );
|
||||||
|
friend ripemd160 operator ^ ( const ripemd160& h1, const ripemd160& h2 );
|
||||||
|
friend bool operator >= ( const ripemd160& h1, const ripemd160& h2 );
|
||||||
|
friend bool operator > ( const ripemd160& h1, const ripemd160& h2 );
|
||||||
|
friend bool operator < ( const ripemd160& h1, const ripemd160& h2 );
|
||||||
|
|
||||||
|
uint32_t _hash[5];
|
||||||
|
};
|
||||||
|
|
||||||
|
class variant;
|
||||||
|
void to_variant( const ripemd160& bi, variant& v );
|
||||||
|
void from_variant( const variant& v, ripemd160& bi );
|
||||||
|
|
||||||
|
} // namespace fc
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template<typename T> struct hash;
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct hash<fc::ripemd160>
|
||||||
|
{
|
||||||
|
size_t operator()( const fc::ripemd160& s )const
|
||||||
|
{
|
||||||
|
return *((size_t*)&s);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
115
src/crypto/ripemd160.cpp
Normal file
115
src/crypto/ripemd160.cpp
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
#include <fc/crypto/hex.hpp>
|
||||||
|
#include <fc/fwd_impl.hpp>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
#include <openssl/ripemd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fc/crypto/ripemd160.hpp>
|
||||||
|
#include <fc/crypto/sha512.hpp>
|
||||||
|
#include <fc/crypto/sha256.hpp>
|
||||||
|
#include <fc/variant.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace fc
|
||||||
|
{
|
||||||
|
|
||||||
|
ripemd160::ripemd160() { memset( _hash, 0, sizeof(_hash) ); }
|
||||||
|
ripemd160::ripemd160( const string& hex_str ) {
|
||||||
|
fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
|
||||||
|
}
|
||||||
|
|
||||||
|
string ripemd160::str()const {
|
||||||
|
return fc::to_hex( (char*)_hash, sizeof(_hash) );
|
||||||
|
}
|
||||||
|
ripemd160::operator string()const { return str(); }
|
||||||
|
|
||||||
|
char* ripemd160::data()const { return (char*)&_hash[0]; }
|
||||||
|
|
||||||
|
|
||||||
|
struct ripemd160::encoder::impl {
|
||||||
|
RIPEMD160_CTX ctx;
|
||||||
|
};
|
||||||
|
|
||||||
|
ripemd160::encoder::~encoder() {}
|
||||||
|
ripemd160::encoder::encoder() {
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
ripemd160 ripemd160::hash( const fc::sha512& h )
|
||||||
|
{
|
||||||
|
return hash( (const char*)&h, sizeof(h) );
|
||||||
|
}
|
||||||
|
ripemd160 ripemd160::hash( const fc::sha256& h )
|
||||||
|
{
|
||||||
|
return hash( (const char*)&h, sizeof(h) );
|
||||||
|
}
|
||||||
|
ripemd160 ripemd160::hash( const char* d, uint32_t dlen ) {
|
||||||
|
encoder e;
|
||||||
|
e.write(d,dlen);
|
||||||
|
return e.result();
|
||||||
|
}
|
||||||
|
ripemd160 ripemd160::hash( const string& s ) {
|
||||||
|
return hash( s.c_str(), s.size() );
|
||||||
|
}
|
||||||
|
|
||||||
|
void ripemd160::encoder::write( const char* d, uint32_t dlen ) {
|
||||||
|
RIPEMD160_Update( &my->ctx, d, dlen);
|
||||||
|
}
|
||||||
|
ripemd160 ripemd160::encoder::result() {
|
||||||
|
ripemd160 h;
|
||||||
|
RIPEMD160_Final((uint8_t*)h.data(), &my->ctx );
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
void ripemd160::encoder::reset() {
|
||||||
|
RIPEMD160_Init( &my->ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
ripemd160 operator << ( const ripemd160& h1, uint32_t i ) {
|
||||||
|
ripemd160 result;
|
||||||
|
uint8_t* r = (uint8_t*)result._hash;
|
||||||
|
uint8_t* s = (uint8_t*)h1._hash;
|
||||||
|
for( uint32_t p = 0; p < sizeof(h1._hash)-1; ++p )
|
||||||
|
r[p] = s[p] << i | (s[p+1]>>(8-i));
|
||||||
|
r[19] = s[19] << i;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
ripemd160 operator ^ ( const ripemd160& h1, const ripemd160& h2 ) {
|
||||||
|
ripemd160 result;
|
||||||
|
result._hash[0] = h1._hash[0] ^ h2._hash[0];
|
||||||
|
result._hash[1] = h1._hash[1] ^ h2._hash[1];
|
||||||
|
result._hash[2] = h1._hash[2] ^ h2._hash[2];
|
||||||
|
result._hash[3] = h1._hash[3] ^ h2._hash[3];
|
||||||
|
result._hash[4] = h1._hash[4] ^ h2._hash[4];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
bool operator >= ( const ripemd160& h1, const ripemd160& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
|
||||||
|
}
|
||||||
|
bool operator > ( const ripemd160& h1, const ripemd160& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
|
||||||
|
}
|
||||||
|
bool operator < ( const ripemd160& h1, const ripemd160& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
|
||||||
|
}
|
||||||
|
bool operator != ( const ripemd160& h1, const ripemd160& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) != 0;
|
||||||
|
}
|
||||||
|
bool operator == ( const ripemd160& h1, const ripemd160& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void to_variant( const ripemd160& bi, variant& v )
|
||||||
|
{
|
||||||
|
v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
|
||||||
|
}
|
||||||
|
void from_variant( const variant& v, ripemd160& bi )
|
||||||
|
{
|
||||||
|
std::vector<char> ve = v.as< std::vector<char> >();
|
||||||
|
if( ve.size() )
|
||||||
|
{
|
||||||
|
memcpy(&bi, ve.data(), fc::min<size_t>(ve.size(),sizeof(bi)) );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
memset( &bi, char(0), sizeof(bi) );
|
||||||
|
}
|
||||||
|
|
||||||
|
} // fc
|
||||||
Loading…
Reference in a new issue