11 static auto test(
int) ->
decltype(std::declval<U>().set_customization(std::declval<std::string>()), std::true_type());
13 static std::false_type test(...);
15 static constexpr bool value = std::is_same<decltype(test<T>(0)), std::true_type>
::value;
22 static auto test(
int) ->
decltype(std::declval<U>().set_personalization(std::declval<std::string>()), std::true_type());
24 static std::false_type test(...);
26 static constexpr bool value = std::is_same<decltype(test<T>(0)), std::true_type>
::value;
33 static auto test(
int) ->
decltype(std::declval<U>().set_salt(std::declval<std::string>()), std::true_type());
35 static std::false_type test(...);
37 static constexpr bool value = std::is_same<decltype(test<T>(0)), std::true_type>
::value;
44 static auto test(
int) ->
decltype(std::declval<U>().set_key(std::declval<std::string>()), std::true_type());
46 static std::false_type test(...);
48 static constexpr bool value = std::is_same<decltype(test<T>(0)), std::true_type>
::value;
54 res.resize(hex.length() / 2);
55 for (
size_t i = 0; i < hex.length(); i += 2)
56 res[i / 2] =
static_cast<char>(strtoul(hex.substr(i, 2).c_str(),
nullptr, 16));
62 str.erase(str.find_last_not_of(
"\r\n\t ") + 1);
63 str.erase(0, str.find_first_not_of(
"\r\n\t "));
66inline std::pair<std::string, std::string>
split_vector(
const std::string& str)
68 std::pair<std::string, std::string> res;
69 auto sep = str.find(
"=");
73 res.first = str.substr(0, sep);
74 res.second = str.substr(sep + 1);
80template<
typename H,
template<
typename>
class M,
typename std::enable_if<!digestpp::detail::is_xof<H>::value>::type* =
nullptr>
86template<
typename H,
template<
typename>
class M,
typename std::enable_if<digestpp::detail::is_xof<H>::value>::type* =
nullptr>
97template<typename H, typename std::enable_if<has_customization<H>::value>::type* =
nullptr>
100 hasher.set_customization(customization);
103template<typename H, typename std::enable_if<has_personalization<H>::value>::type* =
nullptr>
106 hasher.set_personalization(customization);
109template<typename H, typename std::enable_if<!has_salt<H>::value>::type* =
nullptr>
114template<typename H, typename std::enable_if<has_salt<H>::value>::type* =
nullptr>
115void set_salt(
const std::string& salt, H& hasher)
117 hasher.set_salt(salt);
120template<typename H, typename std::enable_if<!has_key<H>::value>::type* =
nullptr>
121void set_key(
const std::string& key, H& hasher)
125template<typename H, typename std::enable_if<has_key<H>::value>::type* =
nullptr>
126void set_key(
const std::string& key, H& hasher)
132void test_vectors(
const H& hasher,
const char* name,
const char* filename)
135 std::ifstream file(filename, std::ios::in);
137 unsigned int count = 0, failed = 0, success = 0;
138 std::string::size_type msgbytes = 0;
139 while (std::getline(file, line))
142 std::string second = splitted.second;
143 if (splitted.first ==
"Bytes")
144 msgbytes = std::stol(splitted.second);
145 if (splitted.first ==
"C")
150 if (splitted.first ==
"Salt")
155 if (splitted.first ==
"Key")
160 if (splitted.first ==
"Msg")
164 copy.absorb(teststr);
165 else while (msgbytes)
167 auto toabsorb = std::min(teststr.size(), msgbytes);
168 copy.absorb(teststr.c_str(), toabsorb);
169 msgbytes -= toabsorb;
172 if (splitted.first ==
"MD")
174 std::transform(second.begin(), second.end(), second.begin(), [](
unsigned char c) { return tolower(c); });
176 if (second != actual)
178 std::cerr <<
"\nError for test " << count <<
"\nExpected: " << second
179 <<
"\nActual: " << actual << std::endl;
187 std::cout << name <<
": ";
189 std::cout << success <<
"/" << count <<
" OK";
190 if (failed && success)
193 std::cout << failed <<
"/" << count <<
" FAILED";
194 if (!success && !failed)
195 std::cout <<
"No tests found. Make sure that file " << filename <<
" exists.";
196 std::cout << std::endl;
Main class template implementing the public API for hashing.
Definition hasher.hpp:38
std::string hexdigest() const
Return hex digest of absorbed data.
Definition hasher.hpp:372
std::string hexsqueeze(size_t len)
Squeeze bytes and return them as a hex string.
Definition hasher.hpp:296
Definition test_vectors.hpp:9
static constexpr bool value
Definition test_vectors.hpp:15
Definition test_vectors.hpp:42
static constexpr bool value
Definition test_vectors.hpp:48
Definition test_vectors.hpp:20
static constexpr bool value
Definition test_vectors.hpp:26
Definition test_vectors.hpp:31
static constexpr bool value
Definition test_vectors.hpp:37
void set_salt(const std::string &salt, H &hasher)
Definition test_vectors.hpp:110
void set_customization(const std::string &customization, H &hasher)
Definition test_vectors.hpp:93
void set_key(const std::string &key, H &hasher)
Definition test_vectors.hpp:121
std::string hex2string(const std::string &hex)
Definition test_vectors.hpp:51
std::pair< std::string, std::string > split_vector(const std::string &str)
Definition test_vectors.hpp:66
void test_vectors(const H &hasher, const char *name, const char *filename)
Definition test_vectors.hpp:132
void trim_string(std::string &str)
Definition test_vectors.hpp:60
std::string compute_vector(const std::string &, digestpp::hasher< H, M > &hasher)
Definition test_vectors.hpp:81