Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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>
7#include <fc/variant.hpp>
8#include "_digest_common.hpp"
9
10namespace fc {
11
12 sha512::sha512() { memset( _hash, 0, sizeof(_hash) ); }
13 sha512::sha512( const string& hex_str ) {
14 auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
15 if( bytes_written < sizeof(_hash) )
16 memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
17 }
18
19 string sha512::str()const {
20 return fc::to_hex( (char*)_hash, sizeof(_hash) );
21 }
22 sha512::operator string()const { return str(); }
23
24 char* sha512::data() { return (char*)&_hash[0]; }
25 const char* sha512::data()const { return (const char*)&_hash[0]; }
26
27
29 SHA512_CTX ctx;
30 };
31
34 reset();
35 }
36
37 sha512 sha512::hash( const char* d, uint32_t dlen ) {
38 encoder e;
39 e.write(d,dlen);
40 return e.result();
41 }
42 sha512 sha512::hash( const string& s ) {
43 return hash( s.c_str(), s.size() );
44 }
45
46 void sha512::encoder::write( const char* d, uint32_t dlen ) {
47 SHA512_Update( &my->ctx, d, dlen);
48 }
50 sha512 h;
51 SHA512_Final((uint8_t*)h.data(), &my->ctx );
52 return h;
53 }
55 SHA512_Init( &my->ctx);
56 }
57
59 sha512 result;
60 fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
61 return result;
62 }
63 sha512 operator ^ ( const sha512& h1, const sha512& h2 ) {
64 sha512 result;
65 result._hash[0] = h1._hash[0] ^ h2._hash[0];
66 result._hash[1] = h1._hash[1] ^ h2._hash[1];
67 result._hash[2] = h1._hash[2] ^ h2._hash[2];
68 result._hash[3] = h1._hash[3] ^ h2._hash[3];
69 result._hash[4] = h1._hash[4] ^ h2._hash[4];
70 result._hash[5] = h1._hash[5] ^ h2._hash[5];
71 result._hash[6] = h1._hash[6] ^ h2._hash[6];
72 result._hash[7] = h1._hash[7] ^ h2._hash[7];
73 return result;
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 bool operator == ( const sha512& h1, const sha512& h2 ) {
88 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0;
89 }
90
91 void to_variant( const sha512& bi, variant& v )
92 {
93 v = std::vector<char>( (const char*)&bi, ((const char*)&bi) + sizeof(bi) );
94 }
95 void from_variant( const variant& v, sha512& bi )
96 {
97 std::vector<char> ve = v.as< std::vector<char> >();
98 if( ve.size() )
99 {
100 memcpy(&bi, ve.data(), fc::min<size_t>(ve.size(),sizeof(bi)) );
101 }
102 else
103 memset( &bi, char(0), sizeof(bi) );
104 }
105
106 template<>
107 unsigned int hmac<sha512>::internal_block_size() const { return 128; }
108}
void write(const char *d, uint32_t dlen)
Definition sha512.cpp:46
friend sha512 operator^(const sha512 &h1, const sha512 &h2)
Definition sha512.cpp:63
friend bool operator<(const sha512 &h1, const sha512 &h2)
Definition sha512.cpp:81
friend bool operator>=(const sha512 &h1, const sha512 &h2)
Definition sha512.cpp:75
friend bool operator!=(const sha512 &h1, const sha512 &h2)
Definition sha512.cpp:84
friend bool operator==(const sha512 &h1, const sha512 &h2)
Definition sha512.cpp:87
friend T & operator<<(T &ds, const sha512 &ep)
Definition sha512.hpp:49
string str() const
Definition sha512.cpp:19
uint64_t _hash[8]
Definition sha512.hpp:67
char * data()
Definition sha512.cpp:24
friend bool operator>(const sha512 &h1, const sha512 &h2)
Definition sha512.cpp:78
static sha512 hash(const char *d, uint32_t dlen)
Definition sha512.cpp:37
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)