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