BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 std::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 std::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 std::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 }
fc::sha512::str
std::string str() const
Definition: sha512.cpp:17
fc::sha512::operator==
friend bool operator==(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:84
fc::from_hex
uint8_t from_hex(char c)
Definition: hex.cpp:6
fc::sha512::sha512
sha512()
Definition: sha512.cpp:12
fc
Definition: api.hpp:15
fc::sha512::operator<
friend bool operator<(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:78
hex.hpp
fc::sha512::encoder
Definition: sha512.hpp:32
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::to_hex
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
sha512.hpp
fc::sha512
Definition: sha512.hpp:9
_digest_common.hpp
fc::sha512::encoder::reset
void reset()
Definition: sha512.cpp:51
fc::sha512::hash
static sha512 hash(const char *d, uint32_t dlen)
Definition: sha512.cpp:34
fc::sha512::operator!=
friend bool operator!=(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:81
fc::sha512::data
char * data() const
Definition: sha512.cpp:22
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::sha512::encoder::impl
Definition: sha512.cpp:25
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::sha512::encoder::impl::ctx
SHA512_CTX ctx
Definition: sha512.cpp:26
fc::sha512::operator>
friend bool operator>(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:75
fc::sha512::operator<<
friend T & operator<<(T &ds, const sha512 &ep)
Definition: sha512.hpp:49
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
hmac.hpp
fc::sha512::encoder::~encoder
~encoder()
Definition: sha512.cpp:29
fc::sha512::encoder::encoder
encoder()
Definition: sha512.cpp:30
variant.hpp
fc::sha512::encoder::result
sha512 result()
Definition: sha512.cpp:46
fc::sha512::_hash
boost::endian::little_uint64_buf_t _hash[8]
Definition: sha512.hpp:67
fc::sha512::operator>=
friend bool operator>=(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:72
fwd_impl.hpp
fc::sha512::operator^
friend sha512 operator^(const sha512 &h1, const sha512 &h2)
Definition: sha512.cpp:60
fc::sha512::data_size
static constexpr size_t data_size()
Definition: sha512.hpp:19
fc::sha512::encoder::write
void write(const char *d, uint32_t dlen)
Definition: sha512.cpp:43