From 7c98e2b345b91d8ef3fb1f7a1bcf74194d54c966 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 12 Mar 2023 12:14:41 +0100 Subject: Use default OPENSSL sha functions if available diff --git a/lib/utils_base.c b/lib/utils_base.c index eb1823b..39032cb 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -402,26 +402,37 @@ int mp_translate_state (char *state_text) { * parse of argv, so that uniqueness in parameters are reflected there. */ char *_np_state_generate_key() { - struct sha256_ctx ctx; int i; char **argv = this_monitoring_plugin->argv; unsigned char result[20]; char keyname[41]; char *p=NULL; +#ifdef USE_OPENSSL + /* + * This code path is chosen if openssl is available (which should be the most common + * scenario). Alternatively, the gnulib implementation/ + * + */ + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + + EVP_DigestInit(ctx, EVP_sha256()); + + for(i=0; iargc; i++) { + EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); + } + + EVP_DigestFinalXOF(ctx, &result, 20); +#else + struct sha256_ctx ctx; - sha256_init_ctx(&ctx); - for(i=0; iargc; i++) { sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); } sha256_finish_ctx(&ctx, &result); - - for (i=0; i<20; ++i) { - sprintf(&keyname[2*i], "%02x", result[i]); - } +#endif // FOUNDOPENSSL keyname[40]='\0'; - + p = strdup(keyname); if(p==NULL) { die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); diff --git a/lib/utils_base.h b/lib/utils_base.h index 5906550..9cb4276 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -2,7 +2,9 @@ #define _UTILS_BASE_ /* Header file for Monitoring Plugins utils_base.c */ -#include "sha256.h" +#ifndef USE_OPENSSL +# include "sha256.h" +#endif /* This file holds header information for thresholds - use this in preference to individual plugin logic */ -- cgit v0.10-9-g596f