diff options
author | lloyd <[email protected]> | 2006-09-02 05:32:28 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-09-02 05:32:28 +0000 |
commit | cc1d7c0010f9e3437813f50b618153c4d3fa29f3 (patch) | |
tree | eb316178f55dccf79eddb25195890f879aa48ac7 /misc/python/src/block.cpp | |
parent | af89b658d84c0a6930f2c6b4ce2c39c5d6ee5dbd (diff) |
Allow for implementing hash functions in Python
Move some code that is used in several places into a common header
Diffstat (limited to 'misc/python/src/block.cpp')
-rw-r--r-- | misc/python/src/block.cpp | 46 |
1 files changed, 11 insertions, 35 deletions
diff --git a/misc/python/src/block.cpp b/misc/python/src/block.cpp index 1b950772c..3e40da6e3 100644 --- a/misc/python/src/block.cpp +++ b/misc/python/src/block.cpp @@ -6,17 +6,7 @@ #include <botan/botan.h> using namespace Botan; -#include <boost/python.hpp> -namespace python = boost::python; - -class Bad_Size : public Exception - { - public: - Bad_Size(const std::string& name, u32bit got, u32bit expected) : - Exception("Bad size encountered in " + name + "; got " + - to_string(got) + " bytes, expected " + to_string(expected)) - {} - }; +#include "common.h" class Py_BlockCipher : public BlockCipher { @@ -27,22 +17,16 @@ class Py_BlockCipher : public BlockCipher void enc(const byte in[], byte out[]) const { - std::string out_str = enc_str( - std::string((const char*)in, BLOCK_SIZE) - ); - if(out_str.size() != BLOCK_SIZE) - throw Bad_Size(name(), out_str.size(), BLOCK_SIZE); - std::memcpy(out, out_str.data(), BLOCK_SIZE); + string2binary( + enc_str(make_string(in, BLOCK_SIZE)), + out, BLOCK_SIZE); } void dec(const byte in[], byte out[]) const { - std::string out_str = dec_str( - std::string((const char*)in, BLOCK_SIZE) - ); - if(out_str.size() != BLOCK_SIZE) - throw Bad_Size(name(), out_str.size(), BLOCK_SIZE); - std::memcpy(out, out_str.data(), BLOCK_SIZE); + string2binary( + dec_str(make_string(in, BLOCK_SIZE)), + out, BLOCK_SIZE); } void key(const byte key[], u32bit len) @@ -59,29 +43,21 @@ class Py_BlockCipher : public BlockCipher std::string encrypt_string(BlockCipher* cipher, const std::string& in) { if(in.size() != cipher->BLOCK_SIZE) - throw Bad_Size(cipher->name(), in.size(), cipher->BLOCK_SIZE); + throw Bad_Size(in.size(), cipher->BLOCK_SIZE); SecureVector<byte> out(cipher->BLOCK_SIZE); cipher->encrypt((const byte*)in.data(), out); - return std::string((const char*)out.begin(), out.size()); + return make_string(out); } std::string decrypt_string(BlockCipher* cipher, const std::string& in) { if(in.size() != cipher->BLOCK_SIZE) - throw Bad_Size(cipher->name(), in.size(), cipher->BLOCK_SIZE); + throw Bad_Size(in.size(), cipher->BLOCK_SIZE); SecureVector<byte> out(cipher->BLOCK_SIZE); cipher->decrypt((const byte*)in.data(), out); - return std::string((const char*)out.begin(), out.size()); - } - -template<typename T> -python::object get_owner(T* me) - { - return python::object( - python::handle<>( - python::borrowed(python::detail::wrapper_base_::get_owner(*me)))); + return make_string(out); } class Wrapped_Block_Cipher : public BlockCipher |