blob: 9a73e0e63d7cad441f86001c87cd95424dde999b [file] [log] [blame]
tylerliu01d63ca2020-10-06 16:29:23 -07001//
2// Created by Tyler on 10/6/20.
3//
4
5#include "assignment-hash.hpp"
6#include <ndn-cxx/util/sha256.hpp>
7
8namespace ndn {
9namespace ndncert {
10
11_LOG_INIT(ndncert.assignment.hash);
12NDNCERT_REGISTER_FUNCFACTORY(AssignmentHash, "hash");
13
14AssignmentHash::AssignmentHash()
15 : NameAssignmentFuncFactory("hash")
16{
17}
18
19NameAssignmentFunc
20AssignmentHash::getFunction(const std::string &factoryParam) {
21 std::list<std::string> paramList;
22 size_t index = 0, startIndex = 0;
23 while ((index = factoryParam.find(":", startIndex)) != factoryParam.npos) {
24 paramList.push_back(factoryParam.substr(startIndex, index));
25 startIndex = index + 1;
26 }
27 if (startIndex != factoryParam.size()) {
28 paramList.push_back(factoryParam.substr(startIndex));
29 }
30 return HashAssignmentFunc(paramList);
31}
32
33AssignmentHash::HashAssignmentFunc::HashAssignmentFunc(std::list<std::string> paramList)
34 : m_paramList(std::move(paramList))
35{}
36
37std::vector<PartialName>
38AssignmentHash::HashAssignmentFunc::operator() (const std::vector<std::tuple<std::string, std::string>> params)
39{
40 if (params.size() > m_paramList.size() * 8) { // might be attack
41 BOOST_THROW_EXCEPTION(std::runtime_error("Too many extra parameters given"));
42 }
43 std::map<std::string, std::string> paramMap;
44 for (const auto& param : params) {
45 paramMap[std::get<0>(param)] = std::get<1>(param);
46 }
47
48 //construct name
49 PartialName name;
50 for (const auto& field : m_paramList) {
51 auto it = paramMap.find(field);
52 if (it == paramMap.end()) {
53 return std::vector<PartialName>();
54 } else {
55 name.append(it->second);
56 }
57 }
58 std::vector<PartialName> nameList;
59 util::Sha256 digest;
60 digest << name.wireEncode();
61 nameList.emplace_back(digest.toString());
62
63 return nameList;
64}
65
66}
67}