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));
}
#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;
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
/// 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.