Source
int zbx_hmac(zbx_crypto_hash_t hash_type, const char *key, size_t key_len, const char *text, size_t text_len,
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
static void *hmac_hash_init(zbx_crypto_hash_t type)
{
void *ctx;
switch (type)
{
case ZBX_HASH_MD5:
ctx = zbx_malloc(NULL, sizeof(md5_state_t));
zbx_md5_init((md5_state_t *)ctx);
break;
case ZBX_HASH_SHA256:
ctx = zbx_malloc(NULL, sizeof(sha256_ctx));
zbx_sha256_init((sha256_ctx *)ctx);
break;
default:
return NULL;
}
return ctx;
}
static void hmac_hash_append(zbx_crypto_hash_t type, void *ctx, const char *text, size_t text_len)
{
switch (type)
{
case ZBX_HASH_MD5:
zbx_md5_append((md5_state_t *)(ctx), (const md5_byte_t *)text, text_len);
break;
case ZBX_HASH_SHA256:
zbx_sha256_process_bytes(text, text_len, (sha256_ctx *)ctx);
break;
default:
return;
}
}
static void hmac_hash_finish(zbx_crypto_hash_t type, void *ctx, char *out)
{
switch (type)
{
case ZBX_HASH_MD5:
zbx_md5_finish((md5_state_t *)(ctx), (md5_byte_t *)out);
break;
case ZBX_HASH_SHA256:
zbx_sha256_finish((sha256_ctx *)ctx, out);