GithubHelp home page GithubHelp logo

hmac_sha256's Introduction

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++)

const std::string str_data = "Hello World!";
const std::string str_key = "super-secret-key";
std::stringstream ss_result;
// 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.

hmac_sha256's People

Contributors

gfabiano avatar h5p9sl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hmac_sha256's Issues

The result is incorrect , when the first value of the key is "00"

Hi,

Please try with these data:

{
/* Test with my golden data. */
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
"\xd6\x36\xff\x79",
"f92a28969656d325bbf23b622bb3cd1815413a80f968a42e8e92bafa0f652896",
},

You will get incorrect result, I tried several key/data pair and found that when the first key value is \x00, then the result becomes incorrect.

I am using : https://cryptii.com/pipes/hmac calculator to check your result.

Thanks,
Bhavik

hmac_sha256 is dangerous

Use hmac_sha256 with caution, H allocates up to datalen + SHA256_BLOCK_SIZE bytes of memory and cannot be used with data streams. The sample code for MD5 in the appendix of RFC 2104 is a better starting point for those looking to implement a streaming-capable version that does not consume all system memory.

Issue with hmac_sha256.c

Hi, thanks for posting your code. When trying to compile I noticed that the hmac_sha256.c file has a problem in the function definition at line 76:

const size_t buflen = xlen + ylen;
uint8_t buf[buflen];

buflen is not a real const, or at least that's how the visual studio compiler sees it.

Getting error running your code

Hi,
Thanks vm for posting this code. When I compiled and ran your code (had to make a small adjustment in concat_and_hash:
uint8_t* buf;
buf = calloc(destlen + srclen, sizeof(uint8_t));
it compiles and works, but the assert throws an error:
Message: Hello World!
HMAC: acde3f64ec328ad72d496afcc86de01509eac15505a4517471b6ed47d1b218dd
Assertion failed: ss_result.str() == "4b393abced1c497f8048860ba1ede46a23f1ff5209b18e9c428bddfbb690aad8", file C:\dev\HMACTest\HMACTest.cpp, line 43

Do you have any idea what is causing this?
Thanks
Jerome

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.