]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Cleanup pdns/digests.hh 13732/head
authorFred Morcos <fred.morcos@open-xchange.com>
Mon, 22 Jan 2024 14:19:13 +0000 (15:19 +0100)
committerFred Morcos <fred.morcos@open-xchange.com>
Mon, 22 Jan 2024 23:39:45 +0000 (00:39 +0100)
.not-formatted
pdns/digests.hh
pdns/dnssecsigner.cc
pdns/test-digests_hh.cc

index ecf20a0344f5c73dd39777d23cd8063ad4fae154..9667e5829e37b5261364b7806407cb004c0c2ed3 100644 (file)
@@ -29,7 +29,6 @@
 ./pdns/dbdnsseckeeper.cc
 ./pdns/delaypipe.cc
 ./pdns/delaypipe.hh
-./pdns/digests.hh
 ./pdns/distributor.hh
 ./pdns/dns.cc
 ./pdns/dns.hh
index 14a926c368e5416b4aaddd83cb96a8d90cd0c84e..29a00145e8252d2fe8abca8092b1a9d29866dbcc 100644 (file)
  */
 #pragma once
 
+#include "config.h"
+#include <memory>
 #include <stdexcept>
 #include <string>
 
 #include <openssl/evp.h>
 
-inline std::string pdns_hash(const EVP_MD * md, const std::string& input)
+namespace pdns
+{
+inline std::string hash(const EVP_MD* messageDigest, const std::string& input)
 {
 #if defined(HAVE_EVP_MD_CTX_NEW) && defined(HAVE_EVP_MD_CTX_FREE)
-  auto mdctx = std::unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>(EVP_MD_CTX_new(), EVP_MD_CTX_free);
+  auto mdctx = std::unique_ptr<EVP_MD_CTX, void (*)(EVP_MD_CTX*)>(EVP_MD_CTX_new(), EVP_MD_CTX_free);
 #else
-  auto mdctx = std::unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
+  auto mdctx = std::unique_ptr<EVP_MD_CTX, void (*)(EVP_MD_CTX*)>(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
 #endif
   if (!mdctx) {
-    throw std::runtime_error(std::string(EVP_MD_name(md)) + " context initialization failed");
+    throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " context initialization failed");
   }
 
-  if (EVP_DigestInit_ex(mdctx.get(), md, nullptr) != 1) {
-    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP initialization failed");
+  if (EVP_DigestInit_ex(mdctx.get(), messageDigest, nullptr) != 1) {
+    throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP initialization failed");
   }
 
   if (EVP_DigestUpdate(mdctx.get(), input.data(), input.size()) != 1) {
-    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP update failed");
+    throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP update failed");
   }
 
-  unsigned int written;
+  unsigned int written = 0;
   std::string result;
-  result.resize(EVP_MD_size(md));
+  result.resize(EVP_MD_size(messageDigest));
 
-  if (EVP_DigestFinal_ex(mdctx.get(), const_cast<unsigned char *>(reinterpret_cast<const unsigned char*>(result.c_str())), &written) != 1) {
-    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP final failed");
+  // NOLINTNEXTLINE(*-cast): Using OpenSSL C APIs.
+  if (EVP_DigestFinal_ex(mdctx.get(), const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(result.c_str())), &written) != 1) {
+    throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP final failed");
   }
 
   if (written != result.size()) {
-    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP final wrote " + std::to_string(written) + ", expected " + std::to_string(result.size()));
+    throw std::runtime_error(std::string(EVP_MD_name(messageDigest)) + " EVP final wrote " + std::to_string(written) + ", expected " + std::to_string(result.size()));
   }
 
   return result;
 }
 
-inline std::string pdns_md5(const std::string& input)
+inline std::string md5(const std::string& input)
 {
-  const auto md = EVP_md5();
-  if (md == nullptr) {
+  const auto* const messageDigest = EVP_md5();
+  if (messageDigest == nullptr) {
     throw std::runtime_error("The MD5 digest is not available via the OpenSSL EVP interface");
   }
 
-  return pdns_hash(md, input);
+  return pdns::hash(messageDigest, input);
 }
 
-inline std::string pdns_sha1(const std::string& input)
+inline std::string sha1(const std::string& input)
 {
-  const auto md = EVP_sha1();
-  if (md == nullptr) {
+  const auto* const messageDigest = EVP_sha1();
+  if (messageDigest == nullptr) {
     throw std::runtime_error("The SHA1 digest is not available via the OpenSSL EVP interface");
   }
 
-  return pdns_hash(md, input);
+  return pdns::hash(messageDigest, input);
+}
 }
index 24489e2b4fb072fcdf7e8395ae534623fb93e0da..e7fd007ffc0510f8cb5c07f7504c5f74573d3294 100644 (file)
@@ -45,10 +45,10 @@ AtomicCounter* g_signatureCount;
 static std::string getLookupKeyFromMessage(const std::string& msg)
 {
   try {
-    return pdns_md5(msg);
+    return pdns::md5(msg);
   }
   catch(const std::runtime_error& e) {
-    return pdns_sha1(msg);
+    return pdns::sha1(msg);
   }
 }
 
index a22bfa8c7b2942973fc4998c88ff0d5f804c5cf1..61b6ff61619fdcfa93a52b2008b8632ca3e6c1fd 100644 (file)
@@ -19,7 +19,7 @@ BOOST_AUTO_TEST_SUITE(test_digests_hh)
 BOOST_AUTO_TEST_CASE(test_pdns_md5sum)
 {
   std::string result = "a3 24 8c e3 1a 88 a6 40 e6 30 73 98 57 6d 06 9e ";
-  std::string sum = pdns_md5("a quick brown fox jumped over the lazy dog");
+  std::string sum = pdns::md5("a quick brown fox jumped over the lazy dog");
 
   BOOST_CHECK_EQUAL(makeHexDump(sum), result);
 }
@@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(test_pdns_md5sum)
 BOOST_AUTO_TEST_CASE(test_pdns_sha1sum)
 {
   std::string result = "b9 37 10 0d c9 57 b3 86 d9 cb 77 fc 90 c0 18 22 fd eb 6e 7f ";
-  std::string sum = pdns_sha1("a quick brown fox jumped over the lazy dog");
+  std::string sum = pdns::sha1("a quick brown fox jumped over the lazy dog");
 
   BOOST_CHECK_EQUAL(makeHexDump(sum), result);
 }