EncryptedStream.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef _ENCRYPTEDSTREAM_H
  2. #define _ENCRYPTEDSTREAM_H 1
  3. namespace bd {
  4. class String;
  5. }
  6. #include <iostream>
  7. #include <bdlib/src/Stream.h>
  8. #include <bdlib/src/String.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #define ENC_AES_256_ECB 1
  13. #define ENC_AES_256_CBC 2
  14. #define ENC_BASE64_BROKEN 4
  15. #define ENC_BASE64 8
  16. #define ENC_KEEP_NEWLINES 16
  17. #define ENC_NO_HEADER 32
  18. #define ENC_DEFAULT (ENC_AES_256_ECB)
  19. class EncryptedStream : public bd::Stream {
  20. private:
  21. bd::String key;
  22. mutable char enc_flags;
  23. void apply_filters(bd::String& buf) const;
  24. void unapply_filters(bd::String& buf) const;
  25. protected:
  26. public:
  27. EncryptedStream(const char* keyStr) : Stream(), key(bd::String(keyStr)), enc_flags(ENC_DEFAULT) {};
  28. EncryptedStream(bd::String& keyStr) : Stream(), key(keyStr), enc_flags(ENC_DEFAULT) {};
  29. EncryptedStream(EncryptedStream& stream) : Stream(stream), key(stream.key), enc_flags(ENC_DEFAULT) {};
  30. void setFlags(const char _enc_flags) const { enc_flags = _enc_flags; }
  31. virtual int loadFile(const int fd);
  32. virtual int writeFile(const int fd) const;
  33. // Overloaded virtuals need to be called to prevent 'hiding' ... good job compiler.
  34. virtual int loadFile(const char* fname) { return bd::Stream::loadFile(fname); }
  35. virtual int writeFile(const char* fname, mode_t mode = (S_IRUSR|S_IWUSR)) const { return bd::Stream::writeFile(fname, mode); }
  36. };
  37. #endif