Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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
10namespace fc
11{
12
13sha1::sha1() { memset( _hash, 0, sizeof(_hash) ); }
14sha1::sha1( const string& hex_str ) {
15 auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
16 if( bytes_written < sizeof(_hash) )
17 memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
18}
19
20string sha1::str()const {
21 return fc::to_hex( (char*)_hash, sizeof(_hash) );
22}
23sha1::operator string()const { return str(); }
24
25char* sha1::data() { return (char*)&_hash[0]; }
26const char* sha1::data()const { return (char*)&_hash[0]; }
27
28
30 SHA_CTX ctx;
31};
32
35 reset();
36}
37
38sha1 sha1::hash( const char* d, uint32_t dlen ) {
39 encoder e;
40 e.write(d,dlen);
41 return e.result();
42}
43sha1 sha1::hash( const string& s ) {
44 return hash( s.c_str(), s.size() );
45}
46
47void sha1::encoder::write( const char* d, uint32_t dlen ) {
48 SHA1_Update( &my->ctx, d, dlen);
49}
51 sha1 h;
52 SHA1_Final((uint8_t*)h.data(), &my->ctx );
53 return h;
54}
56 SHA1_Init( &my->ctx);
57}
58
59sha1 operator << ( const sha1& h1, uint32_t i ) {
60 sha1 result;
61 fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
62 return result;
63}
64sha1 operator ^ ( const sha1& h1, const sha1& h2 ) {
65 sha1 result;
66 result._hash[0] = h1._hash[0] ^ h2._hash[0];
67 result._hash[1] = h1._hash[1] ^ h2._hash[1];
68 result._hash[2] = h1._hash[2] ^ h2._hash[2];
69 result._hash[3] = h1._hash[3] ^ h2._hash[3];
70 result._hash[4] = h1._hash[4] ^ h2._hash[4];
71 return result;
72}
73bool operator >= ( const sha1& h1, const sha1& h2 ) {
74 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
75}
76bool operator > ( const sha1& h1, const sha1& h2 ) {
77 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
78}
79bool operator < ( const sha1& h1, const sha1& h2 ) {
80 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
81}
82bool operator != ( const sha1& h1, const sha1& h2 ) {
83 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) != 0;
84}
85bool operator == ( const sha1& h1, const sha1& h2 ) {
86 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
87}
88
89 void to_variant( const sha1& bi, variant& v )
90 {
91 v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
92 }
93 void from_variant( const variant& v, sha1& bi )
94 {
95 std::vector<char> ve = v.as< std::vector<char> >();
96 if( ve.size() )
97 {
98 memcpy(&bi, ve.data(), fc::min<size_t>(ve.size(),sizeof(bi)) );
99 }
100 else
101 memset( &bi, char(0), sizeof(bi) );
102 }
103
104} // fc
void write(const char *d, uint32_t dlen)
Definition sha1.cpp:47
sha1 result()
Definition sha1.cpp:50
uint32_t _hash[5]
Definition sha1.hpp:66
friend sha1 operator^(const sha1 &h1, const sha1 &h2)
Definition sha1.cpp:64
friend T & operator<<(T &ds, const sha1 &ep)
Definition sha1.hpp:48
friend bool operator>(const sha1 &h1, const sha1 &h2)
Definition sha1.cpp:76
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:85
friend bool operator>=(const sha1 &h1, const sha1 &h2)
Definition sha1.cpp:73
sha1()
Definition sha1.cpp:13
char * data()
Definition sha1.cpp:25
string str() const
Definition sha1.cpp:20
static sha1 hash(const char *d, uint32_t dlen)
Definition sha1.cpp:38
friend bool operator!=(const sha1 &h1, const sha1 &h2)
Definition sha1.cpp:82
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
T as() const
Definition variant.hpp:327
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
uint8_t from_hex(char c)
Definition hex.cpp:6
const T & min(const T &a, const T &b)
Definition utility.hpp:140
fc::string to_hex(const char *d, uint32_t s)
Definition hex.cpp:17
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
CK_ULONG d
char * s
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)