aboutsummaryrefslogtreecommitdiffstats
path: root/misc/python
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-09-29 22:47:15 +0000
committerlloyd <[email protected]>2006-09-29 22:47:15 +0000
commitfb7c04afce612d4d667e4ee1252204ba922b356b (patch)
treede1f34aaf36d4cec5e80b34b9add7c9905c0c036 /misc/python
parent268b1f3089a9692d9a4166a81375930cb1897401 (diff)
Split up all the 'base' algorithm classes into different files
Significant improvements to the public key wrappers.
Diffstat (limited to 'misc/python')
-rw-r--r--misc/python/src/core.cpp11
-rw-r--r--misc/python/src/macs.cpp (renamed from misc/python/src/algos.cpp)50
-rw-r--r--misc/python/src/pk.cpp72
-rw-r--r--misc/python/src/stream.cpp54
4 files changed, 116 insertions, 71 deletions
diff --git a/misc/python/src/core.cpp b/misc/python/src/core.cpp
index 649b48a88..118de81c1 100644
--- a/misc/python/src/core.cpp
+++ b/misc/python/src/core.cpp
@@ -9,7 +9,10 @@ using namespace Botan;
#include <boost/python.hpp>
namespace python = boost::python;
-extern void export_basic_algos();
+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();
@@ -29,7 +32,11 @@ BOOST_PYTHON_MODULE(_botan)
.value("encryption", ENCRYPTION)
.value("decryption", DECRYPTION);
- export_basic_algos();
+ export_block_ciphers();
+ export_stream_ciphers();
+ export_hash_functions();
+ export_macs();
+
export_filters();
export_pk();
export_x509();
diff --git a/misc/python/src/algos.cpp b/misc/python/src/macs.cpp
index 23dd969ff..0a8f1d418 100644
--- a/misc/python/src/algos.cpp
+++ b/misc/python/src/macs.cpp
@@ -9,38 +9,6 @@ using namespace Botan;
#include <boost/python.hpp>
namespace python = boost::python;
-class Py_StreamCipher
- {
- public:
- u32bit keylength_min() const { return cipher->MINIMUM_KEYLENGTH; }
- u32bit keylength_max() const { return cipher->MAXIMUM_KEYLENGTH; }
- u32bit keylength_mod() const { return cipher->KEYLENGTH_MULTIPLE; }
-
- void set_key(const OctetString& key) { cipher->set_key(key); }
- bool valid_keylength(u32bit kl) const
- {
- return cipher->valid_keylength(kl);
- }
-
- std::string name() const { return cipher->name(); }
- void clear() throw() { cipher->clear(); }
-
- std::string crypt(const std::string& in) const
- {
- SecureVector<byte> out(in.size());
- cipher->encrypt((const byte*)in.data(), out.begin(), in.size());
- return std::string((const char*)out.begin(), out.size());
- }
-
- Py_StreamCipher(const std::string& name)
- {
- cipher = get_stream_cipher(name);
- }
- ~Py_StreamCipher() { delete cipher; }
- private:
- StreamCipher* cipher;
- };
-
class Py_MAC
{
public:
@@ -75,24 +43,8 @@ class Py_MAC
MessageAuthenticationCode* mac;
};
-extern void export_block_ciphers();
-extern void export_hash_functions();
-
-void export_basic_algos()
+void export_macs()
{
- export_block_ciphers();
- export_hash_functions();
-
- python::class_<Py_StreamCipher>("StreamCipher", python::init<std::string>())
- .add_property("keylength_min", &Py_StreamCipher::keylength_min)
- .add_property("keylength_max", &Py_StreamCipher::keylength_max)
- .add_property("keylength_mod", &Py_StreamCipher::keylength_mod)
- .add_property("name", &Py_StreamCipher::name)
- .def("clear", &Py_StreamCipher::clear)
- .def("valid_keylength", &Py_StreamCipher::valid_keylength)
- .def("set_key", &Py_StreamCipher::set_key)
- .def("crypt", &Py_StreamCipher::crypt);
-
python::class_<Py_MAC>("MAC", python::init<std::string>())
.add_property("output_length", &Py_MAC::output_length)
.add_property("keylength_min", &Py_MAC::keylength_min)
diff --git a/misc/python/src/pk.cpp b/misc/python/src/pk.cpp
index 1d327c6f5..8c873a487 100644
--- a/misc/python/src/pk.cpp
+++ b/misc/python/src/pk.cpp
@@ -12,38 +12,70 @@ using namespace Botan;
#include <boost/python.hpp>
namespace python = boost::python;
-std::string DER_encode_str(const X509_PublicKey* key)
+std::string encode_pub(const Public_Key* key,
+ const std::string& type)
{
- Pipe pipe;
- X509::encode(*key, pipe, RAW_BER);
- return pipe.read_all_as_string();
+ if(type == "DER")
+ {
+ Pipe pipe;
+ pipe.start_msg();
+ X509::encode(*key, pipe, RAW_BER);
+ pipe.end_msg();
+ return pipe.read_all_as_string();
+ }
+ else if(type == "PEM")
+ return X509::PEM_encode(*key);
+ else
+ throw Encoding_Error("Unknown key encoding method " + type);
}
-std::string get_oid_str(const X509_PublicKey* key)
+std::string encode_priv(const Private_Key* key,
+ const std::string& type)
{
- try
- {
- return key->get_oid().as_string();
- }
- catch(Lookup_Error)
+ if(type == "DER")
{
- return "";
+ Pipe pipe;
+ PKCS8::encode(*key, pipe, RAW_BER);
+ return pipe.read_all_as_string();
}
+ else if(type == "PEM")
+ return PKCS8::PEM_encode(*key);
+ else
+ throw Encoding_Error("Unknown key encoding method " + type);
}
-X509_PublicKey* load_key_str(const std::string& file)
+Private_Key* load_priv(const std::string& file, const std::string& pass)
+ {
+ return PKCS8::load_key(file, pass);
+ }
+
+Public_Key* load_public(const std::string& file)
{
return X509::load_key(file);
}
void export_pk()
{
- python::class_<X509_PublicKey, boost::noncopyable>
- ("X509_PublicKey", python::no_init)
- .def("__init__", python::make_constructor(load_key_str))
- .add_property("algo", &X509_PublicKey::algo_name)
- .add_property("max_input_bits", &X509_PublicKey::max_input_bits)
- .add_property("oid", &get_oid_str)
- .def("__str__", &X509::PEM_encode)
- .def("der_encode", &DER_encode_str);
+ python::class_<Public_Key, boost::noncopyable>
+ ("Public_Key", python::no_init)
+ .add_property("name", &Public_Key::algo_name)
+ .add_property("max_input_bits", &Public_Key::max_input_bits)
+ .def("public_key", encode_pub);
+
+ python::class_<Private_Key, python::bases<Public_Key>, boost::noncopyable>
+ ("Private_Key", python::no_init)
+ .def("private_key", encode_priv);
+
+ python::def("private_key", load_priv,
+ python::return_value_policy<python::manage_new_object>());
+ python::def("public_key", load_public,
+ python::return_value_policy<python::manage_new_object>());
+
+ python::class_<RSA_PublicKey, python::bases<Public_Key> >
+ ("RSA_PublicKey", python::no_init);
+ python::class_<DSA_PublicKey, python::bases<Public_Key> >
+ ("DSA_PublicKey", python::no_init);
+
+ python::class_<RSA_PrivateKey, python::bases<RSA_PublicKey, Private_Key> >
+ ("RSA_PrivateKey", python::init<u32bit>());
}
diff --git a/misc/python/src/stream.cpp b/misc/python/src/stream.cpp
new file mode 100644
index 000000000..c0f07d222
--- /dev/null
+++ b/misc/python/src/stream.cpp
@@ -0,0 +1,54 @@
+/*************************************************
+* Boost.Python module definition *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/botan.h>
+using namespace Botan;
+
+#include "common.h"
+
+class Py_StreamCipher
+ {
+ public:
+ u32bit keylength_min() const { return cipher->MINIMUM_KEYLENGTH; }
+ u32bit keylength_max() const { return cipher->MAXIMUM_KEYLENGTH; }
+ u32bit keylength_mod() const { return cipher->KEYLENGTH_MULTIPLE; }
+
+ void set_key(const OctetString& key) { cipher->set_key(key); }
+ bool valid_keylength(u32bit kl) const
+ {
+ return cipher->valid_keylength(kl);
+ }
+
+ std::string name() const { return cipher->name(); }
+ void clear() throw() { cipher->clear(); }
+
+ std::string crypt(const std::string& in) const
+ {
+ SecureVector<byte> out(in.size());
+ cipher->encrypt((const byte*)in.data(), out.begin(), in.size());
+ return std::string((const char*)out.begin(), out.size());
+ }
+
+ Py_StreamCipher(const std::string& name)
+ {
+ cipher = get_stream_cipher(name);
+ }
+ ~Py_StreamCipher() { delete cipher; }
+ private:
+ StreamCipher* cipher;
+ };
+
+void export_stream_ciphers()
+ {
+ python::class_<Py_StreamCipher>("StreamCipher", python::init<std::string>())
+ .add_property("keylength_min", &Py_StreamCipher::keylength_min)
+ .add_property("keylength_max", &Py_StreamCipher::keylength_max)
+ .add_property("keylength_mod", &Py_StreamCipher::keylength_mod)
+ .add_property("name", &Py_StreamCipher::name)
+ .def("clear", &Py_StreamCipher::clear)
+ .def("valid_keylength", &Py_StreamCipher::valid_keylength)
+ .def("set_key", &Py_StreamCipher::set_key)
+ .def("crypt", &Py_StreamCipher::crypt);
+ }