adding sha224
This commit is contained in:
parent
bf94caf864
commit
726e25d537
3 changed files with 174 additions and 0 deletions
|
|
@ -105,6 +105,7 @@ set( fc_sources
|
||||||
src/crypto/hex.cpp
|
src/crypto/hex.cpp
|
||||||
src/crypto/sha1.cpp
|
src/crypto/sha1.cpp
|
||||||
src/crypto/sha256.cpp
|
src/crypto/sha256.cpp
|
||||||
|
src/crypto/sha224.cpp
|
||||||
src/crypto/sha512.cpp
|
src/crypto/sha512.cpp
|
||||||
src/crypto/dh.cpp
|
src/crypto/dh.cpp
|
||||||
src/crypto/blowfish.cpp
|
src/crypto/blowfish.cpp
|
||||||
|
|
|
||||||
73
include/fc/crypto/sha224.hpp
Normal file
73
include/fc/crypto/sha224.hpp
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
#pragma once
|
||||||
|
#include <fc/fwd.hpp>
|
||||||
|
#include <fc/string.hpp>
|
||||||
|
|
||||||
|
namespace fc
|
||||||
|
{
|
||||||
|
|
||||||
|
class sha224
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
sha224();
|
||||||
|
explicit sha224( const string& hex_str );
|
||||||
|
|
||||||
|
string str()const;
|
||||||
|
operator string()const;
|
||||||
|
|
||||||
|
char* data()const;
|
||||||
|
|
||||||
|
static sha224 hash( const char* d, uint32_t dlen );
|
||||||
|
static sha224 hash( const string& );
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static sha224 hash( const T& t )
|
||||||
|
{
|
||||||
|
sha224::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();
|
||||||
|
sha224 result();
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct impl;
|
||||||
|
fc::fwd<impl,112> my;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline friend T& operator<<( T& ds, const sha224& ep ) {
|
||||||
|
ds.write( ep.data(), sizeof(ep) );
|
||||||
|
return ds;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline friend T& operator>>( T& ds, sha224& ep ) {
|
||||||
|
ds.read( ep.data(), sizeof(ep) );
|
||||||
|
return ds;
|
||||||
|
}
|
||||||
|
friend sha224 operator << ( const sha224& h1, uint32_t i );
|
||||||
|
friend bool operator == ( const sha224& h1, const sha224& h2 );
|
||||||
|
friend bool operator != ( const sha224& h1, const sha224& h2 );
|
||||||
|
friend sha224 operator ^ ( const sha224& h1, const sha224& h2 );
|
||||||
|
friend bool operator >= ( const sha224& h1, const sha224& h2 );
|
||||||
|
friend bool operator > ( const sha224& h1, const sha224& h2 );
|
||||||
|
friend bool operator < ( const sha224& h1, const sha224& h2 );
|
||||||
|
|
||||||
|
uint64_t _hash[3];
|
||||||
|
uint32_t _hash4;
|
||||||
|
};
|
||||||
|
|
||||||
|
class variant;
|
||||||
|
void to_variant( const sha224& bi, variant& v );
|
||||||
|
void from_variant( const variant& v, sha224& bi );
|
||||||
|
|
||||||
|
} // fc
|
||||||
100
src/crypto/sha224.cpp
Normal file
100
src/crypto/sha224.cpp
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
#include <fc/crypto/hex.hpp>
|
||||||
|
#include <fc/fwd_impl.hpp>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <fc/crypto/sha224.hpp>
|
||||||
|
#include <fc/variant.hpp>
|
||||||
|
|
||||||
|
namespace fc {
|
||||||
|
|
||||||
|
sha224::sha224() { memset( _hash, 0, sizeof(_hash) ); }
|
||||||
|
sha224::sha224( const string& hex_str ) {
|
||||||
|
fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
|
||||||
|
}
|
||||||
|
|
||||||
|
string sha224::str()const {
|
||||||
|
return fc::to_hex( (char*)_hash, sizeof(_hash) );
|
||||||
|
}
|
||||||
|
sha224::operator string()const { return str(); }
|
||||||
|
|
||||||
|
char* sha224::data()const { return (char*)&_hash[0]; }
|
||||||
|
|
||||||
|
|
||||||
|
struct sha224::encoder::impl {
|
||||||
|
SHA256_CTX ctx;
|
||||||
|
};
|
||||||
|
|
||||||
|
sha224::encoder::~encoder() {}
|
||||||
|
sha224::encoder::encoder() {
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
sha224 sha224::hash( const char* d, uint32_t dlen ) {
|
||||||
|
encoder e;
|
||||||
|
e.write(d,dlen);
|
||||||
|
return e.result();
|
||||||
|
}
|
||||||
|
sha224 sha224::hash( const string& s ) {
|
||||||
|
return hash( s.c_str(), s.size() );
|
||||||
|
}
|
||||||
|
|
||||||
|
void sha224::encoder::write( const char* d, uint32_t dlen ) {
|
||||||
|
SHA224_Update( &my->ctx, d, dlen);
|
||||||
|
}
|
||||||
|
sha224 sha224::encoder::result() {
|
||||||
|
sha224 h;
|
||||||
|
SHA224_Final((uint8_t*)h.data(), &my->ctx );
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
void sha224::encoder::reset() {
|
||||||
|
SHA224_Init( &my->ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
sha224 operator << ( const sha224& h1, uint32_t i ) {
|
||||||
|
sha224 result;
|
||||||
|
uint8_t* r = (uint8_t*)&result;//result._hash;
|
||||||
|
uint8_t* s = (uint8_t*)&h1;//h1._hash;
|
||||||
|
for( uint32_t p = 0; p < sizeof(sha224)-1; ++p )
|
||||||
|
r[p] = s[p] << i | (s[p+1]>>(8-i));
|
||||||
|
r[sizeof(sha224)-1] = s[sizeof(sha224)-1] << i;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
sha224 operator ^ ( const sha224& h1, const sha224& h2 ) {
|
||||||
|
sha224 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._hash4 = h1._hash4 ^ h2._hash4;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
bool operator >= ( const sha224& h1, const sha224& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(sha224) ) >= 0;
|
||||||
|
}
|
||||||
|
bool operator > ( const sha224& h1, const sha224& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(sha224) ) > 0;
|
||||||
|
}
|
||||||
|
bool operator < ( const sha224& h1, const sha224& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(sha224) ) < 0;
|
||||||
|
}
|
||||||
|
bool operator != ( const sha224& h1, const sha224& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(sha224) ) != 0;
|
||||||
|
}
|
||||||
|
bool operator == ( const sha224& h1, const sha224& h2 ) {
|
||||||
|
return memcmp( h1._hash, h2._hash, sizeof(sha224) ) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void to_variant( const sha224& bi, variant& v )
|
||||||
|
{
|
||||||
|
v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
|
||||||
|
}
|
||||||
|
void from_variant( const variant& v, sha224& 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) );
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue