Update README.md

This commit is contained in:
Tyler 2020-01-15 23:21:00 -07:00 committed by GitHub
parent 6eae3bc0dc
commit 76ac6669de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 4 deletions

View File

@ -13,28 +13,29 @@ A SHA256 HMAC implementation in C/C++
#define SHA256_HASHLEN 32 #define SHA256_HASHLEN 32
int main() { int main() {
// This is what the hmac_sha256 value should output
const std::string testvector = "b0344c61d8db38535ca8afceafbf12b881dc20c9833da726e9376c2e32cff7"; const std::string testvector = "b0344c61d8db38535ca8afceafbf12b881dc20c9833da726e9376c2e32cff7";
const std::string str_data = "Hi There"; const std::string str_data = "Hi There";
std::vector<uint8_t> key, data, out; std::vector<uint8_t> key, data, out;
std::stringstream result; std::stringstream result;
// Allocate memory // Allocate memory using C++ std::vector
key.resize(20, 0x0b); key.resize(20, 0x0b);
data.resize(str_data.length(), 0); data.resize(str_data.length(), 0);
out.resize(SHA256_HASHLEN, 0); out.resize(SHA256_HASHLEN, 0);
// Fill data // Fill `data` with `str_data`'s bytes
data.assign(str_data.cbegin(), str_data.cend()); data.assign(str_data.cbegin(), str_data.cend());
// Call hmac sha256 function // Call hmac-sha256 function
hmac_sha256( hmac_sha256(
key.data(), key.size(), key.data(), key.size(),
data.data(), data.size(), data.data(), data.size(),
out.data(), out.size() out.data(), out.size()
); );
// Convert 'out' to string // Convert `out` to string with std::hex
for (size_t i = 0; i < out.size(); i++) { for (size_t i = 0; i < out.size(); i++) {
result << std::hex << (int)out[i]; result << std::hex << (int)out[i];
} }