Source
int zbx_hmac(zbx_crypto_hash_t hash_type, const char *key, size_t key_len, const char *text, size_t text_len,
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
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)
{