From e07a91db029d4f93976f3ccb9bc28703e9dd7892 Mon Sep 17 00:00:00 2001 From: Tyler <21267024+h5p9sl@users.noreply.github.com> Date: Thu, 4 Jul 2019 20:25:55 -0600 Subject: [PATCH] Create README.md --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf3de3e --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# [hmac_sha256](https://github.com/h5p9sl/hmac_sha256) +A SHA256 HMAC implementation in C/C++ + +## Example (C++) +```cpp +#include "hmac_sha256.h" + +#include +#include +#include +#include + +int main(void) { + static const char* testvector = "b0344c61d8db38535ca8afceafbf12b881dc20c9833da726e9376c2e32cff7"; + uint8_t* key, *data, *out; + unsigned keylen, datalen, outlen; + std::stringstream result; + + // Allocate memory + keylen = 20; + datalen = 8; + outlen = 32; + key = (uint8_t*)malloc(keylen); + data = (uint8_t*)malloc(datalen); + out = (uint8_t*)malloc(outlen); + + // Initialize variables + strncpy((char*)data, "Hi There", datalen); + memset(key, 0x0b, 20); + + hmac_sha256(key, keylen, data, datalen, out, outlen); + + // Convert 'out' to string + for (unsigned i = 0; i < outlen; i++) { + result << std::hex << (int)out[i]; + } + std::cout << result.str() << std::endl; + std::cout << testvector << std::endl; + + // Compare result + if (strncmp(testvector, result.str().c_str(), result.str().length()) == 0) { + std::cout << "Test passed!" << std::endl; + } else { + std::cout << "Test failed." << std::endl; + } + + return 0; +} +```