MCPcopy Create free account
hub / github.com/ElementsProject/elements / TestVector

Function TestVector

src/test/crypto_tests.cpp:30–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29template<typename Hasher, typename In, typename Out>
30static 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
58static void TestSHA1(const std::string &in, const std::string &hexout) { TestVector(CSHA1(), in, ParseHex(hexout));}
59static void TestSHA256(const std::string &in, const std::string &hexout) { TestVector(CSHA256(), in, ParseHex(hexout));}

Callers 6

TestSHA1Function · 0.70
TestSHA256Function · 0.70
TestSHA512Function · 0.70
TestRIPEMD160Function · 0.70
TestHMACSHA256Function · 0.70
TestHMACSHA512Function · 0.70

Calls 6

InsecureRandRangeFunction · 0.85
sizeMethod · 0.45
resizeMethod · 0.45
FinalizeMethod · 0.45
WriteMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected