IBR-DTNSuite  0.8
ibrcommon/ibrcommon/ssl/CipherStream.h
Go to the documentation of this file.
00001 /*
00002  * CipherStream.h
00003  *
00004  *  Created on: 13.07.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef CIPHERSTREAM_H_
00009 #define CIPHERSTREAM_H_
00010 
00011 #include "ibrcommon/Exceptions.h"
00012 #include <streambuf>
00013 #include <iostream>
00014 
00015 namespace ibrcommon
00016 {
00017         class CipherStream : public std::basic_streambuf<char, std::char_traits<char> >, public std::ostream
00018         {
00019         public:
00020                 enum CipherMode
00021                 {
00022                         CIPHER_ENCRYPT = 0,
00023                         CIPHER_DECRYPT = 1
00024                 };
00025 
00026                 CipherStream(std::ostream &stream, const CipherMode mode = CIPHER_DECRYPT, const size_t buffer = 2048);
00027                 virtual ~CipherStream();
00028 
00035                 void encrypt(std::iostream& stream);
00036 
00045                 void decrypt(std::iostream& stream);
00046 
00047         protected:
00048                 virtual void encrypt(char *buf, const size_t size) = 0;
00049                 virtual void decrypt(char *buf, const size_t size) = 0;
00050                 virtual void encrypt_final() {};
00051                 virtual void decrypt_final() {};
00052 
00053                 virtual int sync();
00054                 virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
00055 
00056                 CipherMode _mode;
00057 
00058         private:
00059                 std::ostream &_stream;
00060 
00061                 // Output buffer
00062                 char *data_buf_;
00063 
00064                 // length of the data buffer
00065                 size_t data_size_;
00066         };
00067 }
00068 
00069 #endif /* CIPHERSTREAM_H_ */