| 28 | |
| 29 | template<typename Hasher, typename In, typename Out> |
| 30 | static void TestVector(const Hasher &h, const In &in, const Out &out) { |
| 31 | Out hash; |
| 32 | BOOST_CHECK(out.size() == h.OUTPUT_SIZE); |
| 33 | hash.resize(out.size()); |
| 34 | { |
| 35 | // Test that writing the whole input string at once works. |
| 36 | Hasher(h).Write((const uint8_t*)in.data(), in.size()).Finalize(hash.data()); |
| 37 | BOOST_CHECK(hash == out); |
| 38 | } |
| 39 | for (int i=0; i<32; i++) { |
| 40 | // Test that writing the string broken up in random pieces works. |
| 41 | Hasher hasher(h); |
| 42 | size_t pos = 0; |
| 43 | while (pos < in.size()) { |
| 44 | size_t len = InsecureRandRange((in.size() - pos + 1) / 2 + 1); |
| 45 | hasher.Write((const uint8_t*)in.data() + pos, len); |
| 46 | pos += len; |
| 47 | if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) { |
| 48 | // Test that writing the rest at once to a copy of a hasher works. |
| 49 | Hasher(hasher).Write((const uint8_t*)in.data() + pos, in.size() - pos).Finalize(hash.data()); |
| 50 | BOOST_CHECK(hash == out); |
| 51 | } |
| 52 | } |
| 53 | hasher.Finalize(hash.data()); |
| 54 | BOOST_CHECK(hash == out); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void TestSHA1(const std::string &in, const std::string &hexout) { TestVector(CSHA1(), in, ParseHex(hexout));} |
| 59 | static void TestSHA256(const std::string &in, const std::string &hexout) { TestVector(CSHA256(), in, ParseHex(hexout));} |
no test coverage detected