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