tylerliu | 01d63ca | 2020-10-06 16:29:23 -0700 | [diff] [blame] | 1 | // |
| 2 | // Created by Tyler on 10/6/20. |
| 3 | // |
| 4 | |
| 5 | #include "assignment-hash.hpp" |
| 6 | #include <ndn-cxx/util/sha256.hpp> |
| 7 | |
| 8 | namespace ndn { |
| 9 | namespace ndncert { |
| 10 | |
| 11 | _LOG_INIT(ndncert.assignment.hash); |
tylerliu | afb27a0 | 2020-10-06 17:20:06 -0700 | [diff] [blame] | 12 | |
tylerliu | 01d63ca | 2020-10-06 16:29:23 -0700 | [diff] [blame] | 13 | NDNCERT_REGISTER_FUNCFACTORY(AssignmentHash, "hash"); |
| 14 | |
| 15 | AssignmentHash::AssignmentHash() |
| 16 | : NameAssignmentFuncFactory("hash") |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | NameAssignmentFunc |
| 21 | AssignmentHash::getFunction(const std::string &factoryParam) { |
| 22 | std::list<std::string> paramList; |
| 23 | size_t index = 0, startIndex = 0; |
tylerliu | 3f63c02 | 2020-10-07 11:08:29 -0700 | [diff] [blame] | 24 | 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 | } |
tylerliu | 01d63ca | 2020-10-06 16:29:23 -0700 | [diff] [blame] | 29 | startIndex = index + 1; |
| 30 | } |
| 31 | if (startIndex != factoryParam.size()) { |
| 32 | paramList.push_back(factoryParam.substr(startIndex)); |
| 33 | } |
tylerliu | 4251013 | 2020-10-07 15:48:02 -0700 | [diff] [blame] | 34 | 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 | } |
tylerliu | 01d63ca | 2020-10-06 16:29:23 -0700 | [diff] [blame] | 42 | |
tylerliu | 4251013 | 2020-10-07 15:48:02 -0700 | [diff] [blame] | 43 | //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()); |
tylerliu | 01d63ca | 2020-10-06 16:29:23 -0700 | [diff] [blame] | 57 | |
tylerliu | 4251013 | 2020-10-07 15:48:02 -0700 | [diff] [blame] | 58 | return nameList; |
| 59 | }; |
tylerliu | 01d63ca | 2020-10-06 16:29:23 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | } |
| 63 | } |