[monitoring-plugins] Fix hash creation

RincewindsHat git at monitoring-plugins.org
Thu Sep 21 12:10:11 CEST 2023


 Module: monitoring-plugins
 Branch: master
 Commit: f6f2ba34c713b5bc65936af836be24ebc74faf46
 Author: RincewindsHat <12514511+RincewindsHat at users.noreply.github.com>
   Date: Sun Mar 12 13:58:25 2023 +0100
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=f6f2ba3

Fix hash creation

---

 lib/utils_base.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/lib/utils_base.c b/lib/utils_base.c
index 39032cb..105ff44 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -404,9 +404,15 @@ int mp_translate_state (char *state_text) {
 char *_np_state_generate_key() {
 	int i;
 	char **argv = this_monitoring_plugin->argv;
-	unsigned char result[20];
 	char keyname[41];
 	char *p=NULL;
+
+	unsigned char *result = malloc(256 * sizeof(unsigned char));
+
+	if (result == NULL) {
+		die(STATE_UNKNOWN, _("Failed to allocate memory for hashes: %s"), strerror(errno));
+	}
+
 #ifdef USE_OPENSSL
 	/*
 	 * This code path is chosen if openssl is available (which should be the most common
@@ -421,16 +427,22 @@ char *_np_state_generate_key() {
 		EVP_DigestUpdate(ctx, argv[i], strlen(argv[i]));
 	}
 
-	EVP_DigestFinalXOF(ctx, &result, 20);
+	EVP_DigestFinal(ctx, result, NULL);
 #else
+
 	struct sha256_ctx ctx;
 
 	for(i=0; i<this_monitoring_plugin->argc; i++) {
 		sha256_process_bytes(argv[i], strlen(argv[i]), &ctx);
 	}
 
-	sha256_finish_ctx(&ctx, &result);
+	sha256_finish_ctx(&ctx, result);
 #endif // FOUNDOPENSSL
+
+	for (i=0; i<20; ++i) {
+		sprintf(&keyname[2*i], "%02x", result[i]);
+	}
+
 	keyname[40]='\0';
 
 	p = strdup(keyname);



More information about the Commits mailing list