Minimal HMAC-SHA256 implementation in C / C++ https://github.com/h5p9sl/hmac_sha256
Go to file
h5p9sl 2496197cef Merge branch 'master' of https://www.github.com/h5p9sl/hmac_sha256 2019-07-04 20:27:37 -06:00
README.md Create README.md 2019-07-04 20:25:55 -06:00
WjCryptLib_Sha256.c Initial Commit 2019-05-19 21:43:18 -06:00
WjCryptLib_Sha256.h Initial Commit 2019-05-19 21:43:18 -06:00
hmac_sha256.c Initial Commit 2019-05-19 21:43:18 -06:00
hmac_sha256.h Add C++ compatibility 2019-07-04 20:27:00 -06:00

README.md

hmac_sha256

A SHA256 HMAC implementation in C/C++

Example (C++)

#include "hmac_sha256.h"

#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>

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;
}