-
-static void
-format_hash(const uint8_t *hash, size_t out_len, uint8_t *out)
-{
- int i;
-
- for (i = 0; i < out_len / 2 - 1; i++)
- sprintf((char *) out + i*2, "%02x", hash[i % 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);
- }
-}