TDME2  1.9.200
SHA256.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <vector>
3 
4 #include <cstring>
5 
6 #include <tdme/tdme.h>
7 #include <ext/sha256/sha256.h>
9 
10 using std::string;
11 using std::vector;
12 
13 void tdme::utilities::SHA256::encode(const string& decodedString, string& encodedString) {
14  unsigned char digest[::SHA256::DIGEST_SIZE];
15  memset(digest, 0, ::SHA256::DIGEST_SIZE);
16 
17  auto ctx = ::SHA256();
18  ctx.init();
19  ctx.update((const uint8_t*)decodedString.c_str(), decodedString.size());
20  ctx.final(digest);
21 
22  char buf[2 * ::SHA256::DIGEST_SIZE + 1];
23  buf[2 * ::SHA256::DIGEST_SIZE] = 0;
24  for (int i = 0; i < ::SHA256::DIGEST_SIZE; i++) sprintf(buf + i * 2, "%02x", digest[i]);
25  encodedString = string(buf);
26 }
27 
28 void tdme::utilities::SHA256::encode(const vector<uint8_t>& decodedData, string& encodedString) {
29  unsigned char digest[::SHA256::DIGEST_SIZE];
30  memset(digest, 0, ::SHA256::DIGEST_SIZE);
31 
32  auto ctx = ::SHA256();
33  ctx.init();
34  ctx.update((const uint8_t*)decodedData.data(), decodedData.size());
35  ctx.final(digest);
36 
37  char buf[2 * ::SHA256::DIGEST_SIZE + 1];
38  buf[2 * ::SHA256::DIGEST_SIZE] = 0;
39  for (int i = 0; i < ::SHA256::DIGEST_SIZE; i++) sprintf(buf + i * 2, "%02x", digest[i]);
40  encodedString = string(buf);
41 }
SHA256 hash class.
Definition: SHA256.h:16
static const string encode(const string &decodedString)
Encodes an string to SHA256 string.
Definition: SHA256.h:23