aboutsummaryrefslogtreecommitdiffstats
path: root/wrappers/boost-python/src/hash.cpp
blob: 69c107c3219d8523de76b990b6fdc2dbccec077b (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*************************************************
* Boost.Python module definition                 *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/botan.h>
using namespace Botan;

#include "common.h"

class Py_HashFunction : public HashFunction
   {
   public:
      virtual void hash_str(const std::string&) = 0;
      virtual std::string final_str() = 0;

      void clear() throw() {}

      void add_data(const byte input[], u32bit length)
         {
         hash_str(make_string(input, length));
         }

      void final_result(byte output[])
         {
         string2binary(final_str(), output, OUTPUT_LENGTH);
         }

      Py_HashFunction(u32bit digest_size, u32bit block_size) :
         HashFunction(digest_size, block_size) {}
   };

class Wrapped_HashFunction : public HashFunction
   {
   public:
      void add_data(const byte in[], u32bit len) { hash->update(in, len); }
      void final_result(byte out[]) { hash->final(out); }

      void clear() throw() {}

      std::string name() const { return hash->name(); }
      HashFunction* clone() const { return hash->clone(); }

      Wrapped_HashFunction(python::object py_obj, HashFunction* h) :
         HashFunction(h->OUTPUT_LENGTH, h->HASH_BLOCK_SIZE),
         obj(py_obj), hash(h) {}
   private:
      python::object obj;
      HashFunction* hash;
   };

class Py_HashFunction_Wrapper : public Py_HashFunction,
                                public python::wrapper<Py_HashFunction>
   {
   public:
      HashFunction* clone() const
         {
         python::object self = get_owner(this);
         python::object py_clone = self.attr("__class__")();
         HashFunction* hf = python::extract<HashFunction*>(py_clone);
         return new Wrapped_HashFunction(py_clone, hf);
         }

      std::string name() const
         {
         return this->get_override("name")();
         }

      void hash_str(const std::string& in)
         {
         this->get_override("update")(in);
         }

      std::string final_str()
         {
         return this->get_override("final")();
         }

      Py_HashFunction_Wrapper(u32bit digest_size, u32bit block_size) :
         Py_HashFunction(digest_size, block_size) {}
   };

std::string final_str(HashFunction* hash)
   {
   SecureVector<byte> digest = hash->final();
   return std::string((const char*)digest.begin(), digest.size());
   }

void export_hash_functions()
   {
   void (HashFunction::*update_str)(const std::string&) =
      &HashFunction::update;

   python::class_<HashFunction, boost::noncopyable>
      ("HashFunction", python::no_init)
      .def("__init__", python::make_constructor(get_hash))
      .def_readonly("digest_size", &HashFunction::OUTPUT_LENGTH)
      .def("name", &HashFunction::name)
      .def("update", update_str)
      .def("final", final_str);

   python::class_<Py_HashFunction_Wrapper, python::bases<HashFunction>,
                  boost::noncopyable>
      ("HashFunctionImpl", python::init<u32bit, u32bit>())
      .def("name", python::pure_virtual(&Py_HashFunction::name))
      .def("update", python::pure_virtual(&Py_HashFunction::hash_str))
      .def("final", python::pure_virtual(&Py_HashFunction::final_str));
   }