diff options
author | lloyd <[email protected]> | 2008-10-12 16:48:48 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-10-12 16:48:48 +0000 |
commit | 2cbbb4881b18408a13c860ac89ab642adb248a6f (patch) | |
tree | f4fb63092ab0311033a2f2140a6ce0cd50131db0 /wrappers/boost-python/src | |
parent | cf884b7cc1012848ea9cc78a09aba12a2c226096 (diff) |
Add a wrapper around Botan's RandomNumberGenerator using Boost.Python, and
add a Python test script for it.
Diffstat (limited to 'wrappers/boost-python/src')
-rw-r--r-- | wrappers/boost-python/src/block.cpp | 2 | ||||
-rw-r--r-- | wrappers/boost-python/src/core.cpp | 44 | ||||
-rw-r--r-- | wrappers/boost-python/src/hash.cpp | 2 | ||||
-rw-r--r-- | wrappers/boost-python/src/python_botan.h (renamed from wrappers/boost-python/src/common.h) | 9 | ||||
-rw-r--r-- | wrappers/boost-python/src/stream.cpp | 2 |
5 files changed, 49 insertions, 10 deletions
diff --git a/wrappers/boost-python/src/block.cpp b/wrappers/boost-python/src/block.cpp index 385376005..185b88811 100644 --- a/wrappers/boost-python/src/block.cpp +++ b/wrappers/boost-python/src/block.cpp @@ -6,7 +6,7 @@ #include <botan/botan.h> using namespace Botan; -#include "common.h" +#include "python_botan.h" class Py_BlockCipher : public BlockCipher { diff --git a/wrappers/boost-python/src/core.cpp b/wrappers/boost-python/src/core.cpp index 3bca5330f..7ef2a9970 100644 --- a/wrappers/boost-python/src/core.cpp +++ b/wrappers/boost-python/src/core.cpp @@ -9,19 +9,49 @@ using namespace Botan; #include <boost/python.hpp> namespace python = boost::python; -extern void export_block_ciphers(); -extern void export_stream_ciphers(); -extern void export_hash_functions(); -extern void export_macs(); -extern void export_filters(); -extern void export_pk(); -extern void export_x509(); +#include "python_botan.h" + +class Python_RandomNumberGenerator + { + public: + Python_RandomNumberGenerator() + { rng = RandomNumberGenerator::make_rng(); } + ~Python_RandomNumberGenerator() { delete rng; } + + std::string name() const { return rng->name(); } + + void reseed() { rng->reseed(); } + + int gen_random_byte() { return rng->next_byte(); } + + std::string gen_random(int n) + { + std::string s(n, 0); + rng->randomize(reinterpret_cast<byte*>(&s[0]), n); + return s; + } + + void add_entropy(const std::string& in) + { rng->add_entropy(reinterpret_cast<const byte*>(in.c_str()), in.length()); } + + private: + RandomNumberGenerator* rng; + }; BOOST_PYTHON_MODULE(_botan) { python::class_<LibraryInitializer>("LibraryInitializer") .def(python::init< python::optional<std::string> >()); + python::class_<Python_RandomNumberGenerator>("RandomNumberGenerator") + .def(python::init<>()) + .def("__str__", &Python_RandomNumberGenerator::name) + .def("name", &Python_RandomNumberGenerator::name) + .def("reseed", &Python_RandomNumberGenerator::reseed) + .def("add_entropy", &Python_RandomNumberGenerator::add_entropy) + .def("gen_random_byte", &Python_RandomNumberGenerator::gen_random_byte) + .def("gen_random", &Python_RandomNumberGenerator::gen_random); + python::class_<OctetString>("OctetString") .def(python::init< python::optional<std::string> >()) //.def(python::init< u32bit >()) diff --git a/wrappers/boost-python/src/hash.cpp b/wrappers/boost-python/src/hash.cpp index 69c107c32..6ef198087 100644 --- a/wrappers/boost-python/src/hash.cpp +++ b/wrappers/boost-python/src/hash.cpp @@ -6,7 +6,7 @@ #include <botan/botan.h> using namespace Botan; -#include "common.h" +#include "python_botan.h" class Py_HashFunction : public HashFunction { diff --git a/wrappers/boost-python/src/common.h b/wrappers/boost-python/src/python_botan.h index 968cc3908..d0fd10207 100644 --- a/wrappers/boost-python/src/common.h +++ b/wrappers/boost-python/src/python_botan.h @@ -8,6 +8,15 @@ using namespace Botan; #include <boost/python.hpp> namespace python = boost::python; + +extern void export_block_ciphers(); +extern void export_stream_ciphers(); +extern void export_hash_functions(); +extern void export_macs(); +extern void export_filters(); +extern void export_pk(); +extern void export_x509(); + class Bad_Size : public Exception { public: diff --git a/wrappers/boost-python/src/stream.cpp b/wrappers/boost-python/src/stream.cpp index 97ea9ae04..f94bd7fd5 100644 --- a/wrappers/boost-python/src/stream.cpp +++ b/wrappers/boost-python/src/stream.cpp @@ -6,7 +6,7 @@ #include <botan/botan.h> using namespace Botan; -#include "common.h" +#include "python_botan.h" class Py_StreamCipher { |