blob: 22b6e6929dcefb2bbb974accc237802bba7f4dca [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);
tylerliuafb27a02020-10-06 17:20:06 -070012
tylerliu01d63ca2020-10-06 16:29:23 -070013NDNCERT_REGISTER_FUNCFACTORY(AssignmentHash, "hash");
14
15AssignmentHash::AssignmentHash()
16 : NameAssignmentFuncFactory("hash")
17{
18}
19
20NameAssignmentFunc
21AssignmentHash::getFunction(const std::string &factoryParam) {
22 std::list<std::string> paramList;
23 size_t index = 0, startIndex = 0;
24 while ((index = factoryParam.find(":", startIndex)) != factoryParam.npos) {
25 paramList.push_back(factoryParam.substr(startIndex, index));
26 startIndex = index + 1;
27 }
28 if (startIndex != factoryParam.size()) {
29 paramList.push_back(factoryParam.substr(startIndex));
30 }
31 return HashAssignmentFunc(paramList);
32}
33
34AssignmentHash::HashAssignmentFunc::HashAssignmentFunc(std::list<std::string> paramList)
35 : m_paramList(std::move(paramList))
36{}
37
38std::vector<PartialName>
39AssignmentHash::HashAssignmentFunc::operator() (const std::vector<std::tuple<std::string, std::string>> params)
40{
41 if (params.size() > m_paramList.size() * 8) { // might be attack
42 BOOST_THROW_EXCEPTION(std::runtime_error("Too many extra parameters given"));
43 }
44 std::map<std::string, std::string> paramMap;
45 for (const auto& param : params) {
46 paramMap[std::get<0>(param)] = std::get<1>(param);
47 }
48
49 //construct name
50 PartialName name;
51 for (const auto& field : m_paramList) {
52 auto it = paramMap.find(field);
53 if (it == paramMap.end()) {
54 return std::vector<PartialName>();
55 } else {
56 name.append(it->second);
57 }
58 }
59 std::vector<PartialName> nameList;
60 util::Sha256 digest;
61 digest << name.wireEncode();
62 nameList.emplace_back(digest.toString());
63
64 return nameList;
65}
66
67}
68}