BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
hmac.hpp
Go to the documentation of this file.
1 /*
2  * File: hmac.hpp
3  * Author: Peter Conrad
4  *
5  * Created on 1. Juli 2015, 21:48
6  */
7 
8 #ifndef HMAC_HPP
9 #define HMAC_HPP
10 
11 #include <fc/crypto/sha224.hpp>
12 #include <fc/crypto/sha256.hpp>
13 #include <fc/crypto/sha512.hpp>
14 
15 namespace fc {
16 
17  template<typename H>
18  class hmac
19  {
20  public:
21  hmac() {}
22 
23  H digest( const char* c, uint32_t c_len, const char* d, uint32_t d_len )
24  {
25  encoder.reset();
26  add_key(c, c_len, 0x36);
27  encoder.write( d, d_len );
28  H intermediate = encoder.result();
29 
30  encoder.reset();
31  add_key(c, c_len, 0x5c);
32  encoder.write( intermediate.data(), intermediate.data_size() );
33  return encoder.result();
34  }
35 
36  private:
37  void add_key( const char* c, const uint32_t c_len, char pad )
38  {
39  if ( c_len > internal_block_size() )
40  {
41  H hash = H::hash( c, c_len );
42  add_key( hash.data(), hash.data_size(), pad );
43  }
44  else
45  for (unsigned int i = 0; i < internal_block_size(); i++ )
46  {
47  encoder.put( pad ^ ((i < c_len) ? *c++ : 0) );
48  }
49  }
50 
51  unsigned int internal_block_size() const;
52 
53  H dummy;
54  typename H::encoder encoder;
55  };
56 
60 }
61 
62 #endif /* HMAC_HPP */
63 
H digest(const char *c, uint32_t c_len, const char *d, uint32_t d_len)
Definition: hmac.hpp:23
hmac()
Definition: hmac.hpp:21
hmac< fc::sha224 > hmac_sha224
Definition: hmac.hpp:57
Definition: api.hpp:15
hmac< fc::sha512 > hmac_sha512
Definition: hmac.hpp:59
hmac< fc::sha256 > hmac_sha256
Definition: hmac.hpp:58