Raven Core  3.0.0
P2P Digital Currency
hmac_sha256.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "crypto/hmac_sha256.h"
7 
8 #include <string.h>
9 
10 CHMAC_SHA256::CHMAC_SHA256(const unsigned char* key, size_t keylen)
11 {
12  unsigned char rkey[64];
13  if (keylen <= 64) {
14  memcpy(rkey, key, keylen);
15  memset(rkey + keylen, 0, 64 - keylen);
16  } else {
17  CSHA256().Write(key, keylen).Finalize(rkey);
18  memset(rkey + 32, 0, 32);
19  }
20 
21  for (int n = 0; n < 64; n++)
22  rkey[n] ^= 0x5c;
23  outer.Write(rkey, 64);
24 
25  for (int n = 0; n < 64; n++)
26  rkey[n] ^= 0x5c ^ 0x36;
27  inner.Write(rkey, 64);
28 }
29 
30 void CHMAC_SHA256::Finalize(unsigned char hash[OUTPUT_SIZE])
31 {
32  unsigned char temp[32];
33  inner.Finalize(temp);
34  outer.Write(temp, 32).Finalize(hash);
35 }
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:202
CSHA256 inner
Definition: hmac_sha256.h:19
CHMAC_SHA256(const unsigned char *key, size_t keylen)
Definition: hmac_sha256.cpp:10
CSHA256 outer
Definition: hmac_sha256.h:18
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:228
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hmac_sha256.cpp:30
void * memcpy(void *a, const void *b, size_t c)
static const size_t OUTPUT_SIZE
Definition: hmac_sha256.h:22
A hasher class for SHA-256.
Definition: sha256.h:14