From: Tomek Mrugalski Date: Tue, 20 Jun 2023 21:42:41 +0000 (+0200) Subject: [#2707] hash calc moved to common method X-Git-Tag: Kea-2.4.0~134 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=86d9466eb73a272ff1b5b8cab9153ba2e90e20d6;p=thirdparty%2Fkea.git [#2707] hash calc moved to common method --- diff --git a/src/bin/dhcp6/ctrl_dhcp6_srv.cc b/src/bin/dhcp6/ctrl_dhcp6_srv.cc index 13c978e5ac..53c07574ac 100644 --- a/src/bin/dhcp6/ctrl_dhcp6_srv.cc +++ b/src/bin/dhcp6/ctrl_dhcp6_srv.cc @@ -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 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)); } diff --git a/src/lib/config/base_command_mgr.cc b/src/lib/config/base_command_mgr.cc index 9b8fab82ec..5a7591c735 100644 --- a/src/lib/config/base_command_mgr.cc +++ b/src/lib/config/base_command_mgr.cc @@ -9,9 +9,13 @@ #include #include #include +#include #include #include +#include +#include #include +#include 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 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 diff --git a/src/lib/config/base_command_mgr.h b/src/lib/config/base_command_mgr.h index 9cc5a7f987..28bad8dc98 100644 --- a/src/lib/config/base_command_mgr.h +++ b/src/lib/config/base_command_mgr.h @@ -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.