Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TrustedCertificates.h"
- #include <stdexcept>
- #include <algorithm>
- #include <array>
- #include <cstring>
- namespace TrustedCertificates
- {
- namespace
- {
- template <char... cs>
- constexpr auto operator"" _bin()
- {
- struct HexadecimalNumberChecker
- {
- bool status{ true };
- int count{ 0 };
- constexpr HexadecimalNumberChecker operator +(char c) const
- {
- if (count == 0) { return { status && c == '0', count + 1 }; }
- if (count == 1) { return { status && (c == 'x' || c == 'X'), count + 1 }; }
- return { status && (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'), count + 1 };
- }
- constexpr operator bool() const { return status && count % 2 == 0; }
- };
- constexpr bool check = (HexadecimalNumberChecker() + ... + cs);
- static_assert(check, "Only hexadecimal number are accepted.");
- if (!check) { throw std::invalid_argument("Only hexadecimal number are accepted."); }
- constexpr std::array<char, sizeof...(cs)> data{ cs... };
- std::array<std::uint8_t, sizeof...(cs) / 2 - 1> result{};
- auto nibble = [](char input)
- {
- if (input >= '0' && input <= '9') return input - '0';
- if (input >= 'A' && input <= 'F') return input - 'A' + 10;
- if (input >= 'a' && input <= 'f') return input - 'a' + 10;
- return 0;
- };
- for (size_t i = 0; i < result.size(); i++)
- {
- result[i] = nibble(data[2 + i * 2]) << 4 | nibble(data[2 + i * 2 + 1]);
- }
- return result;
- }
- template <class T, size_t SZ>
- constexpr bool is_sorted(const T (&x)[SZ])
- {
- for (size_t i = 1; i < SZ; i++)
- {
- if (x[i] < x[i - 1]) { return false; }
- }
- return true;
- }
- }
- bool IsRegistered(const uint8_t sha1[20], const uint8_t md5[16])
- {
- struct CertEntry
- {
- std::array<uint8_t, 20> sha1{};
- std::array<uint8_t, 16> md5{};
- constexpr bool operator <(const CertEntry& e) const
- {
- for (size_t i = 0; i < sha1.size(); i++) { if (sha1[i] != e.sha1[i]) { return sha1[i] < e.sha1[i]; } }
- for (size_t i = 0; i < md5.size(); i++) { if (md5[i] != e.md5[i]) { return md5[i] < e.md5[i]; } }
- return false;
- }
- };
- CertEntry e{};
- memcpy(e.sha1.data(), sha1, 20);
- memcpy(e.md5.data(), md5, 16);
- constexpr static CertEntry db[] =
- {
- #define CERT_ENTRY(SHA1,MD5) [](){ constexpr CertEntry x = { 0x ## SHA1 ##_bin, 0x ## MD5 ## _bin }; return x; }()
- #include "trusted_auth_root_hash_list.txt"
- #undef CERT_ENTRY
- };
- static_assert(is_sorted(db), "db must be sorted.");
- return std::binary_search(std::begin(db), std::end(db), e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement