-
-static void
-_format_hash(const uint8_t *hash, size_t out_len, uint8_t *out)
-{
- int ir, iw;
-
- for (ir = 0, iw = 0; iw <= out_len - 3; ir++, iw += 2)
- sprintf((char *) out + iw, "%02x", hash[ir % SHA256_DIGEST_SIZE]);
-}
-
-static void
-_hash(const uint8_t *in,
- const uint8_t *key,
- size_t out_len,
- uint8_t *out)
-{
- if (key == NULL) {
- struct sha256_ctx ctx;
- uint8_t hash[SHA256_DIGEST_SIZE];
-
- sha256_init(&ctx);
- sha256_update(&ctx, strlen((char *) in), in);
- sha256_digest(&ctx, sizeof(hash), hash);
- _format_hash(hash, out_len, out);
- }
- else {
- struct hmac_sha256_ctx ctx;
- uint8_t hash[SHA256_DIGEST_SIZE];
-
- hmac_sha256_set_key(&ctx, strlen((char *) key), key);
- hmac_sha256_update(&ctx, strlen((char *) in), in);
- hmac_sha256_digest(&ctx, sizeof(hash), hash);
- _format_hash(hash, out_len, out);
- }
-}