]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2707] hash calc moved to common method
authorTomek Mrugalski <tomek@isc.org>
Tue, 20 Jun 2023 21:42:41 +0000 (23:42 +0200)
committerTomek Mrugalski <tomek@isc.org>
Thu, 22 Jun 2023 14:23:38 +0000 (16:23 +0200)
src/bin/dhcp6/ctrl_dhcp6_srv.cc
src/lib/config/base_command_mgr.cc
src/lib/config/base_command_mgr.h

index 13c978e5ac922099dd4928d862ef13c93d6030b2..53c07574ac1ecf5f0cb4778de1c7cb69ca75a64e 100644 (file)
@@ -297,21 +297,11 @@ ControlledDhcpv6Srv::commandConfigGetHandler(const string&,
 ConstElementPtr
 ControlledDhcpv6Srv::commandConfigHashGetHandler(const string&,
                                                  ConstElementPtr /*args*/) {
-    ConstElementPtr config = CfgMgr::instance().getCurrentCfg()->toElement();
-    // Assume that config is never null.
-    string config_txt = config->str();
-    OutputBuffer hash_data(0);
-    isc::cryptolink::digest(config_txt.c_str(),
-                            config_txt.size(),
-                            isc::cryptolink::HashAlgorithm::SHA256,
-                            hash_data);
-    vector<uint8_t> hash;
-    hash.resize(hash_data.getLength());
-    if (hash.size() > 0) {
-        memmove(&hash[0], hash_data.getData(), hash.size());
-    }
+    ElementPtr config = CfgMgr::instance().getCurrentCfg()->toElement();
+    string hash = BaseCommandMgr::getHash(config);
+
     ElementPtr params = Element::createMap();
-    params->set("hash", Element::create(encode::encodeHex(hash)));
+    params->set("hash", Element::create(hash));
     return (createAnswer(CONTROL_RESULT_SUCCESS, params));
 }
 
index 9b8fab82ece12df70f5833b07328ac1b409e371f..5a7591c735f5d258a8cabfd685b77e9870c91781 100644 (file)
@@ -9,9 +9,13 @@
 #include <cc/command_interpreter.h>
 #include <config/base_command_mgr.h>
 #include <config/config_log.h>
+#include <cryptolink/crypto_hash.h>
 #include <hooks/callout_handle.h>
 #include <hooks/hooks_manager.h>
+#include <util/encode/hex.h>
+#include <util/buffer.h>
 #include <functional>
+#include <vector>
 
 using namespace isc::data;
 using namespace isc::hooks;
@@ -186,6 +190,29 @@ BaseCommandMgr::listCommandsHandler(const std::string& /* name */,
     return (createAnswer(CONTROL_RESULT_SUCCESS, commands));
 }
 
+std::string
+BaseCommandMgr::getHash(isc::data::ElementPtr& config) {
+
+    // First, get the string representation.
+    std::string config_txt = config->str();
+    isc::util::OutputBuffer hash_data(0);
+    isc::cryptolink::digest(config_txt.c_str(),
+                            config_txt.size(),
+                            isc::cryptolink::HashAlgorithm::SHA256,
+                            hash_data);
+
+    // Now we need to convert this to output buffer to vector, which can be accepted
+    // by encodeHex().
+    std::vector<uint8_t> hash;
+    hash.resize(hash_data.getLength());
+    if (hash.size() > 0) {
+        memmove(&hash[0], hash_data.getData(), hash.size());
+    }
+
+    // Now encode the value as base64 and we're done here.
+    return (isc::util::encode::encodeHex(hash));
+}
+
 
 } // namespace isc::config
 } // namespace isc
index 9cc5a7f987501601102ad9ab5d16902623143cd7..28bad8dc98a650c5e27f992607b66947d5186e00 100644 (file)
@@ -153,6 +153,14 @@ public:
     /// handled at all times.
     void deregisterAll();
 
+    /// @brief returns a hash of a given Element structure
+    ///
+    /// The hash is currently implemented as SHA256 on the string represenation of the structure.
+    ///
+    /// @param config typically full config, but hash can be calculated on any structure
+    /// @return string representation
+    static std::string getHash(isc::data::ElementPtr& config);
+
 protected:
 
     /// @brief Handles the command having a given name and arguments.