BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 std::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 std::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 std::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
fc::sha1::operator^
friend sha1 operator^(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:61
fc::sha1::encoder
Definition: sha1.hpp:33
fc::sha1::encoder::reset
void reset()
Definition: sha1.cpp:52
fc::sha1::_hash
boost::endian::little_uint32_buf_t _hash[5]
Definition: sha1.hpp:68
fc::sha1::operator>
friend bool operator>(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:73
fc::from_hex
uint8_t from_hex(char c)
Definition: hex.cpp:6
fc::sha1::operator>=
friend bool operator>=(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:70
fc
Definition: api.hpp:15
fc::sha1::operator==
friend bool operator==(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:82
hex.hpp
fc::sha1::operator!=
friend bool operator!=(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:79
fc::sha1::data_size
static constexpr size_t data_size()
Definition: sha1.hpp:20
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::sha1::encoder::result
sha1 result()
Definition: sha1.cpp:47
fc::to_hex
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17
_digest_common.hpp
sha1.hpp
fc::sha1::hash
static sha1 hash(const char *d, uint32_t dlen)
Definition: sha1.cpp:35
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::sha1::str
std::string str() const
Definition: sha1.cpp:18
fc::sha1::operator<
friend bool operator<(const sha1 &h1, const sha1 &h2)
Definition: sha1.cpp:76
fc::sha1::encoder::impl::ctx
SHA_CTX ctx
Definition: sha1.cpp:27
fc::sha1
Definition: sha1.hpp:10
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::sha1::sha1
sha1()
Definition: sha1.cpp:13
fc::sha1::encoder::~encoder
~encoder()
Definition: sha1.cpp:30
fc::sha1::encoder::impl
Definition: sha1.cpp:26
fc::sha1::encoder::encoder
encoder()
Definition: sha1.cpp:31
variant.hpp
fc::sha1::operator<<
friend T & operator<<(T &ds, const sha1 &ep)
Definition: sha1.hpp:50
fc::sha1::data
char * data() const
Definition: sha1.cpp:23
fwd_impl.hpp
fc::sha1::encoder::write
void write(const char *d, uint32_t dlen)
Definition: sha1.cpp:44