# HG changeset patch # User Kim Alvefur # Date 1656082794 -7200 # Node ID fc6213104d78014b4cfcf55430deb539465bb76a # Parent 96871ee6e26c113e39d9782a32e88df5b3b05035 util.hashes: Revert to HMAC() convenience function Reverts some of 1e41dd0f8353 Seems HMAC() isn't deprecated after all? Must have been at some point according to #1589 Twice as fast for some reason. diff -r 96871ee6e26c -r fc6213104d78 util-src/hashes.c --- a/util-src/hashes.c Fri Jun 24 16:49:03 2022 +0200 +++ b/util-src/hashes.c Fri Jun 24 16:59:54 2022 +0200 @@ -129,33 +129,15 @@ static int Levp_hmac(lua_State *L, const EVP_MD *evp) { unsigned char hash[EVP_MAX_MD_SIZE], result[EVP_MAX_MD_SIZE * 2]; size_t key_len, msg_len; - size_t out_len = EVP_MAX_MD_SIZE; + unsigned int out_len = EVP_MAX_MD_SIZE; const char *key = luaL_checklstring(L, 1, &key_len); const char *msg = luaL_checklstring(L, 2, &msg_len); const int hex_out = lua_toboolean(L, 3); - EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, (unsigned char *)key, key_len); - EVP_MD_CTX *ctx = EVP_MD_CTX_new(); - - if(ctx == NULL || pkey == NULL) { - goto fail; - } - - if(!EVP_DigestSignInit(ctx, NULL, evp, NULL, pkey)) { + if(HMAC(evp, key, key_len, (const unsigned char*)msg, msg_len, (unsigned char*)hash, &out_len) == NULL) { goto fail; } - if(!EVP_DigestSignUpdate(ctx, msg, msg_len)) { - goto fail; - } - - if(!EVP_DigestSignFinal(ctx, hash, &out_len)) { - goto fail; - } - - EVP_MD_CTX_free(ctx); - EVP_PKEY_free(pkey); - if(hex_out) { toHex(hash, out_len, result); lua_pushlstring(L, (char *)result, out_len * 2); @@ -166,8 +148,6 @@ return 1; fail: - EVP_MD_CTX_free(ctx); - EVP_PKEY_free(pkey); return luaL_error(L, ERR_error_string(ERR_get_error(), NULL)); }