BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
sha512.cpp
Go to the documentation of this file.
1 #include <fc/crypto/hex.hpp>
2 #include <fc/crypto/hmac.hpp>
3 #include <fc/fwd_impl.hpp>
4 #include <openssl/sha.h>
5 #include <string.h>
6 #include <fc/crypto/sha512.hpp>
7 #include <fc/variant.hpp>
8 #include "_digest_common.hpp"
9 
10 namespace fc {
11 
12  sha512::sha512() { memset( _hash, 0, sizeof(_hash) ); }
13  sha512::sha512( const string& hex_str ) {
14  fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
15  }
16 
17  string sha512::str()const {
18  return fc::to_hex( (char*)_hash, sizeof(_hash) );
19  }
20  sha512::operator string()const { return str(); }
21 
22  char* sha512::data()const { return (char*)&_hash[0]; }
23 
24 
26  SHA512_CTX ctx;
27  };
28 
31  reset();
32  }
33 
34  sha512 sha512::hash( const char* d, uint32_t dlen ) {
35  encoder e;
36  e.write(d,dlen);
37  return e.result();
38  }
39  sha512 sha512::hash( const string& s ) {
40  return hash( s.c_str(), s.size() );
41  }
42 
43  void sha512::encoder::write( const char* d, uint32_t dlen ) {
44  SHA512_Update( &my->ctx, d, dlen);
45  }
47  sha512 h;
48  SHA512_Final((uint8_t*)h.data(), &my->ctx );
49  return h;
50  }
52  SHA512_Init( &my->ctx);
53  }
54 
55  sha512 operator << ( const sha512& h1, uint32_t i ) {
56  sha512 result;
57  fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
58  return result;
59  }
60  sha512 operator ^ ( const sha512& h1, const sha512& h2 ) {
61  sha512 result;
62  result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value();
63  result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value();
64  result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value();
65  result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value();
66  result._hash[4] = h1._hash[4].value() ^ h2._hash[4].value();
67  result._hash[5] = h1._hash[5].value() ^ h2._hash[5].value();
68  result._hash[6] = h1._hash[6].value() ^ h2._hash[6].value();
69  result._hash[7] = h1._hash[7].value() ^ h2._hash[7].value();
70  return result;
71  }
72  bool operator >= ( const sha512& h1, const sha512& h2 ) {
73  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
74  }
75  bool operator > ( const sha512& h1, const sha512& h2 ) {
76  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
77  }
78  bool operator < ( const sha512& h1, const sha512& h2 ) {
79  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
80  }
81  bool operator != ( const sha512& h1, const sha512& h2 ) {
82  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) != 0;
83  }
84  bool operator == ( const sha512& h1, const sha512& h2 ) {
85  return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
86  }
87 
88  void to_variant( const sha512& bi, variant& v, uint32_t max_depth )
89  {
90  to_variant( std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) ), v, max_depth );
91  }
92  void from_variant( const variant& v, sha512& bi, uint32_t max_depth )
93  {
94  std::vector<char> ve = v.as< std::vector<char> >( max_depth );
95  memset( &bi, char(0), sizeof(bi) );
96  if( ve.size() )
97  memcpy( &bi, ve.data(), std::min<size_t>(ve.size(),sizeof(bi)) );
98  }
99 
100  template<>
101  unsigned int hmac<sha512>::internal_block_size() const { return 128; }
102 }
std::string str() const
Definition: sha512.cpp:17
uint8_t from_hex(char c)
Definition: hex.cpp:6
boost::endian::little_uint64_buf_t _hash[8]
Definition: sha512.hpp:67
friend bool operator>=(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:72
T as(uint32_t max_depth) const
Definition: variant.hpp:336
friend bool operator==(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:84
static sha512 hash(const char *d, uint32_t dlen)
Definition: sha512.cpp:34
friend bool operator!=(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:81
friend sha512 operator^(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:60
friend bool operator>(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:75
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
char * data() const
Definition: sha512.cpp:22
static constexpr size_t data_size()
Definition: sha512.hpp:19
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
sha512 result()
Definition: sha512.cpp:46
friend bool operator<(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:78
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
Definition: api.hpp:15
void write(const char *d, uint32_t dlen)
Definition: sha512.cpp:43
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
friend T & operator<<(T &ds, const sha512 &ep)
Definition: sha512.hpp:49