EncryptedStream.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_CBC)
  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 bd::String& IV) const;
  24. void unapply_filters(bd::String& buf, const bd::String& IV) 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. inline void setFlags(const char _enc_flags) const noexcept {
  31. enc_flags = _enc_flags;
  32. }
  33. virtual int loadFile(const int fd);
  34. using bd::Stream::loadFile;
  35. virtual int writeFile(const int fd) const;
  36. using bd::Stream::writeFile;
  37. };
  38. #endif