diff options
author | lloyd <[email protected]> | 2006-08-31 17:43:20 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-08-31 17:43:20 +0000 |
commit | fdb896dfa9c45bada38f3d7f9472e82cc6f685f9 (patch) | |
tree | 8ff9449cde2540e1ebd6ae2c255baba13a8dea0c /misc/python/src | |
parent | b8844f2bfca984285f9cfe61bed56cf5d9c33705 (diff) |
Add simple wrappers for block cipher objects
Diffstat (limited to 'misc/python/src')
-rw-r--r-- | misc/python/src/block.cpp | 79 | ||||
-rw-r--r-- | misc/python/src/core.cpp | 9 |
2 files changed, 85 insertions, 3 deletions
diff --git a/misc/python/src/block.cpp b/misc/python/src/block.cpp new file mode 100644 index 000000000..d68300ce6 --- /dev/null +++ b/misc/python/src/block.cpp @@ -0,0 +1,79 @@ +/************************************************* +* Boost.Python module definition * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#include <botan/botan.h> +using namespace Botan; + +#include <boost/python.hpp> +namespace python = boost::python; + +/* Encrypt or decrypt */ +std::string process(const std::string& in, BlockCipher* cipher, + void (BlockCipher::* proc)(const byte[], byte[]) const) + { + if(in.size() != cipher->BLOCK_SIZE) + throw Invalid_Argument("Input for cipher " + cipher->name() + + " must be " + to_string(cipher->BLOCK_SIZE) + + " bytes"); + + const byte* in_data = (const byte*)in.data(); + + SecureVector<byte> out(cipher->BLOCK_SIZE); + (cipher->*proc)(in_data, out); + + return std::string((const char*)out.begin(), out.size()); + } + +class Py_BlockCipher + { + public: + u32bit block_size() const { return cipher->BLOCK_SIZE; } + u32bit keylength_min() const { return cipher->MINIMUM_KEYLENGTH; } + u32bit keylength_max() const { return cipher->MAXIMUM_KEYLENGTH; } + u32bit keylength_mod() const { return cipher->KEYLENGTH_MULTIPLE; } + + bool valid_keylength(u32bit kl) const + { + return cipher->valid_keylength(kl); + } + + std::string encrypt(const std::string& in) const + { + return process(in, cipher, &BlockCipher::encrypt); + } + std::string decrypt(const std::string& in) const + { + return process(in, cipher, &BlockCipher::decrypt); + } + + void set_key(const OctetString& key) + { + cipher->set_key(key); + } + + Py_BlockCipher(const std::string& name) + { + cipher = get_block_cipher(name); + } + ~Py_BlockCipher() + { + delete cipher; + } + private: + BlockCipher* cipher; + }; + +void export_block_ciphers() + { + python::class_<Py_BlockCipher>("BlockCipher", python::init<std::string>()) + .add_property("block_size", &Py_BlockCipher::block_size) + .add_property("keylength_min", &Py_BlockCipher::keylength_min) + .add_property("keylength_max", &Py_BlockCipher::keylength_max) + .add_property("keylength_mod", &Py_BlockCipher::keylength_mod) + .def("valid_keylength", &Py_BlockCipher::valid_keylength) + .def("set_key", &Py_BlockCipher::set_key) + .def("encrypt", &Py_BlockCipher::encrypt) + .def("decrypt", &Py_BlockCipher::decrypt); + } diff --git a/misc/python/src/core.cpp b/misc/python/src/core.cpp index 07252e720..5669a5b83 100644 --- a/misc/python/src/core.cpp +++ b/misc/python/src/core.cpp @@ -3,14 +3,15 @@ * (C) 1999-2006 The Botan Project * *************************************************/ -#include <botan/init.h> -#include <botan/symkey.h> +#include <botan/botan.h> using namespace Botan; #include <boost/python.hpp> namespace python = boost::python; +extern void export_block_ciphers(); extern void export_filters(); +extern void export_pk(); extern void export_x509(); BOOST_PYTHON_MODULE(_botan) @@ -22,12 +23,14 @@ BOOST_PYTHON_MODULE(_botan) .def(python::init< python::optional<std::string> >()) .def(python::init< u32bit >()) .def("__str__", &OctetString::as_string) - .add_property("length", &OctetString::length); + .def("__len__", &OctetString::length); python::enum_<Cipher_Dir>("cipher_dir") .value("encryption", ENCRYPTION) .value("decryption", DECRYPTION); + export_block_ciphers(); export_filters(); + export_pk(); export_x509(); } |