aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_hash.cpp
blob: 55510b9f44b9161d3ecfaba8297b37b4de0b15ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "tests.h"

#include <botan/libstate.h>
#include <botan/hash.h>
#include <botan/hex.h>
#include <iostream>
#include <fstream>

using namespace Botan;

namespace {

size_t hash_test(const std::string& algo,
                 const std::string& in_hex,
                 const std::string& out_hex)
   {
   Algorithm_Factory& af = global_state().algorithm_factory();

   const auto providers = af.providers_of(algo);
   size_t fails = 0;

   for(auto provider: providers)
      {
      auto proto = af.prototype_hash_function(algo, provider);

      if(!proto)
         {
         std::cout << "Unable to get " << algo << " from " << provider << "\n";
         ++fails;
         continue;
         }

      std::unique_ptr<HashFunction> hash(proto->clone());

      hash->update(hex_decode(in_hex));

      auto h = hash->final();

      if(h != hex_decode_locked(out_hex))
         {
         std::cout << algo << " " << provider << " got " << hex_encode(h) << " != " << out_hex << "\n";
         ++fails;
         }
      }

   return fails;
   }

}

size_t test_hash()
   {
   std::ifstream vec(TEST_DATA_DIR "/hash.vec");

   return run_tests_bb(vec, "Hash", "Out", true,
             [](std::map<std::string, std::string> m) -> size_t
             {
             return hash_test(m["Hash"], m["In"], m["Out"]);
             });
   }