EncryptedStream.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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{ENC_DEFAULT};
  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) : key(bd::String(keyStr)) {};
  28. EncryptedStream(const bd::String& keyStr) : key(keyStr) {};
  29. EncryptedStream(bd::String&& keyStr) noexcept :
  30. key(std::move(keyStr)) {};
  31. EncryptedStream(const EncryptedStream& stream) = default;
  32. EncryptedStream(EncryptedStream&& stream) noexcept = default;
  33. inline void setFlags(const char _enc_flags) const noexcept {
  34. enc_flags = _enc_flags;
  35. }
  36. virtual int loadFile(const int fd);
  37. using bd::Stream::loadFile;
  38. virtual int writeFile(const int fd) const;
  39. using bd::Stream::writeFile;
  40. };
  41. #endif