aboutsummaryrefslogtreecommitdiffstats
path: root/wrappers/boost-python
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-10-12 16:48:48 +0000
committerlloyd <[email protected]>2008-10-12 16:48:48 +0000
commit2cbbb4881b18408a13c860ac89ab642adb248a6f (patch)
treef4fb63092ab0311033a2f2140a6ce0cd50131db0 /wrappers/boost-python
parentcf884b7cc1012848ea9cc78a09aba12a2c226096 (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')
-rw-r--r--wrappers/boost-python/Makefile10
-rwxr-xr-xwrappers/boost-python/rng_test.py22
-rw-r--r--wrappers/boost-python/src/block.cpp2
-rw-r--r--wrappers/boost-python/src/core.cpp44
-rw-r--r--wrappers/boost-python/src/hash.cpp2
-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.cpp2
7 files changed, 76 insertions, 15 deletions
diff --git a/wrappers/boost-python/Makefile b/wrappers/boost-python/Makefile
index 0f5ca68d2..e3a1acf37 100644
--- a/wrappers/boost-python/Makefile
+++ b/wrappers/boost-python/Makefile
@@ -2,8 +2,8 @@ CXX = g++
LANG_FLAGS = -fPIC -Wall -Wno-unused -ftemplate-depth-255
OPT_FLAGS = -g -Os
-PYTHON_ROOT = /usr/lib/python2.4/config
-PYTHON_INC = -I/usr/include/python2.4
+PYTHON_ROOT = /usr/lib/python2.5/config
+PYTHON_INC = -I/usr/include/python2.5
PYTHON_DEF = -DBOOST_PYTHON_DYNAMIC_LIB -DBOOST_PYTHON_SOURCE
WRAPPER_CFLAGS = $(shell botan-config --cflags)
@@ -12,14 +12,14 @@ SHARED_CFLAGS = $(LANG_FLAGS) $(OPT_FLAGS) $(PYTHON_INC)
BOOST_CFLAGS = $(PYTHON_DEF) $(SHARED_CFLAGS)
WRAP_SRC = $(wildcard src/*.cpp)
-WRAP_OBJS = $(patsubst src/%.cpp,%.o,$(WRAP_SRC))
+WRAP_OBJS = $(patsubst src/%.cpp,build/%.o,$(WRAP_SRC))
all: botan/_botan.so
-%.o: src/%.cpp
+build/%.o: src/%.cpp
$(CXX) -Isrc/ $(SHARED_CFLAGS) $(WRAPPER_CFLAGS) -c $< -o $@
-botan/_botan.so: $(WRAP_OBJS)
+botan/_botan.so: $(WRAP_OBJS)
$(CXX) -shared -o $@ $(shell botan-config --libs) -L$(PYTHON_ROOT) $(WRAP_OBJS) -lboost_python -Wl,-rpath-link,. -Wl,-soname,$@
clean:
diff --git a/wrappers/boost-python/rng_test.py b/wrappers/boost-python/rng_test.py
new file mode 100755
index 000000000..06c79b84e
--- /dev/null
+++ b/wrappers/boost-python/rng_test.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+
+import botan
+
+rng = botan.RandomNumberGenerator()
+
+print "name", rng.name()
+
+rng.add_entropy("blah")
+
+print "random 16", rng.gen_random(16).encode("hex")
+print "random 32", rng.gen_random(32).encode("base64"),
+
+rng.reseed()
+
+for i in range(0, 10):
+ print rng.gen_random_byte(),
+print
+
+rng.add_entropy("blah")
+
+print "random 16", rng.gen_random(16).encode("hex")
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
{