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; |
| 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 | |
| 34 | AssignmentHash::HashAssignmentFunc::HashAssignmentFunc(std::list<std::string> paramList) |
| 35 | : m_paramList(std::move(paramList)) |
| 36 | {} |
| 37 | |
| 38 | std::vector<PartialName> |
| 39 | AssignmentHash::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 | } |