Minimal HMAC-SHA256 implementation in C / C++ https://github.com/h5p9sl/hmac_sha256
Go to file
Tyler a4def9767b
Add license (#5)
2022-04-01 17:17:49 +00:00
examples Add C example, and remove SIZEOFARRAY macro 2021-11-02 01:57:43 -06:00
.clang-format Format code using clang-format 2021-08-18 16:39:37 -06:00
CMakeLists.txt added compiles directive with cmakelists 2022-01-15 17:32:02 +01:00
LICENSE Add license (#5) 2022-04-01 17:17:49 +00:00
README.md Add return values, clean code & update README 2021-04-21 14:00:38 -06:00
hmac_sha256.c Add C example, and remove SIZEOFARRAY macro 2021-11-02 01:57:43 -06:00
hmac_sha256.h Format code using clang-format 2021-08-18 16:39:37 -06:00
sha256.c Format code using clang-format 2021-08-18 16:39:37 -06:00
sha256.h Format code using clang-format 2021-08-18 16:39:37 -06:00

README.md

hmac_sha256

Minimal HMAC-SHA256 implementation in C / C++

This repository provides minimal HMAC-Sha256 code you can copy into your own projects. The hmac_sha256 function looks like this:

size_t // Returns the number of bytes written to `out`
hmac_sha256(
    // [in]: The key and its length.
    //      Should be at least 32 bytes long for optimal security.
    const void* key, const size_t keylen,

    // [in]: The data to hash alongside the key.
    const void* data, const size_t datalen,

    // [out]: The output hash.
    //      Should be 32 bytes long. If it's less than 32 bytes,
    //      the resulting hash will be truncated to the specified length.
    void* out, const size_t outlen
);

Contributing

All contributions are welcome, feature requests, or issues. I aim to tailor this code not only for myself, but for other's use cases too.

Usage Example (C++)

79a57d2a85/examples/simple_example.cpp (L13-L26)

    const std::string str_data = "Hello World!";
    const std::string str_key = "super-secret-key";

    // Allocate memory for the HMAC
    std::vector<uint8_t> out(SHA256_HASH_SIZE);

    // Call hmac-sha256 function
    hmac_sha256(
        str_key.data(),  str_key.size(),
        str_data.data(), str_data.size(),
        out.data(),  out.size()
    );

Sha256 Implementation

Big thank you to WjCryptLib for providing the Sha256 implementation of which this project is based off. If you need more public domain cryptographic functions in C (sha, aes, md5), check them out.