blob: ebcf6d0a86731e1af7734cefda989ed8a1f212a3 [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 }
tylerliu42510132020-10-07 15:48:02 -070034 return [paramList](const std::vector<std::tuple<std::string, std::string>> params){
35 if (params.size() > paramList.size() * 8) { // might be attack
36 BOOST_THROW_EXCEPTION(std::runtime_error("Too many extra parameters given"));
37 }
38 std::map<std::string, std::string> paramMap;
39 for (const auto& param : params) {
40 paramMap[std::get<0>(param)] = std::get<1>(param);
41 }
tylerliu01d63ca2020-10-06 16:29:23 -070042
tylerliu42510132020-10-07 15:48:02 -070043 //construct name
44 PartialName name;
45 for (const auto& field : paramList) {
46 auto it = paramMap.find(field);
47 if (it == paramMap.end()) {
48 return std::vector<PartialName>();
49 } else {
50 name.append(it->second);
51 }
52 }
53 std::vector<PartialName> nameList;
54 util::Sha256 digest;
55 digest << name.wireEncode();
56 nameList.emplace_back(digest.toString());
tylerliu01d63ca2020-10-06 16:29:23 -070057
tylerliu42510132020-10-07 15:48:02 -070058 return nameList;
59 };
tylerliu01d63ca2020-10-06 16:29:23 -070060}
61
62}
63}