blob: 22b6e6929dcefb2bbb974accc237802bba7f4dca [file] [log] [blame]
//
// Created by Tyler on 10/6/20.
//
#include "assignment-hash.hpp"
#include <ndn-cxx/util/sha256.hpp>
namespace ndn {
namespace ndncert {
_LOG_INIT(ndncert.assignment.hash);
NDNCERT_REGISTER_FUNCFACTORY(AssignmentHash, "hash");
AssignmentHash::AssignmentHash()
: NameAssignmentFuncFactory("hash")
{
}
NameAssignmentFunc
AssignmentHash::getFunction(const std::string &factoryParam) {
std::list<std::string> paramList;
size_t index = 0, startIndex = 0;
while ((index = factoryParam.find(":", startIndex)) != factoryParam.npos) {
paramList.push_back(factoryParam.substr(startIndex, index));
startIndex = index + 1;
}
if (startIndex != factoryParam.size()) {
paramList.push_back(factoryParam.substr(startIndex));
}
return HashAssignmentFunc(paramList);
}
AssignmentHash::HashAssignmentFunc::HashAssignmentFunc(std::list<std::string> paramList)
: m_paramList(std::move(paramList))
{}
std::vector<PartialName>
AssignmentHash::HashAssignmentFunc::operator() (const std::vector<std::tuple<std::string, std::string>> params)
{
if (params.size() > m_paramList.size() * 8) { // might be attack
BOOST_THROW_EXCEPTION(std::runtime_error("Too many extra parameters given"));
}
std::map<std::string, std::string> paramMap;
for (const auto& param : params) {
paramMap[std::get<0>(param)] = std::get<1>(param);
}
//construct name
PartialName name;
for (const auto& field : m_paramList) {
auto it = paramMap.find(field);
if (it == paramMap.end()) {
return std::vector<PartialName>();
} else {
name.append(it->second);
}
}
std::vector<PartialName> nameList;
util::Sha256 digest;
digest << name.wireEncode();
nameList.emplace_back(digest.toString());
return nameList;
}
}
}