/******************************************************************************/ /* SlunkCrypt, by LoRd_MuldeR */ /* This work has been released under the CC0 1.0 Universal license! */ /******************************************************************************/ #ifndef INC_SLUNKCRYPT_PLUSPLUS #define INC_SLUNKCRYPT_PLUSPLUS #include "slunkcrypt.h" #include #include #include class SlunkCryptEncr { public: SlunkCryptEncr(const std::string &passwd) { if (slunkcrypt_generate_nonce(&m_nonce) != SLUNKCRYPT_SUCCESS) { throw std::runtime_error("Failed to generate the seed value!"); } if ((m_instance = slunkcrypt_alloc(m_nonce, (const uint8_t*)passwd.c_str(), passwd.length())) == SLUNKCRYPT_NULL) { throw std::runtime_error("Failed to create encoder instance!"); } } SlunkCryptEncr(SlunkCryptEncr &&other) noexcept { this->m_instance = other.m_instance; this->m_nonce = other.m_nonce; other.m_instance = SLUNKCRYPT_NULL; } ~SlunkCryptEncr(void) { if (m_instance != SLUNKCRYPT_NULL) { slunkcrypt_free(m_instance); } } bool encrypt(const uint8_t* const input, uint8_t* const output, size_t length) { return (slunkcrypt_encrypt(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS); } bool encrypt(const std::vector &input, std::vector &output) { if (output.size() >= input.size()) { return (slunkcrypt_encrypt(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS); } return false; } bool encrypt_inplace(uint8_t* const buffer, size_t length) { return (slunkcrypt_encrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS); } bool encrypt_inplace(std::vector &buffer) { return (slunkcrypt_encrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS); } uint64_t get_nonce(void) const { return m_nonce; } private: SlunkCryptEncr(const SlunkCryptEncr&); SlunkCryptEncr& operator=(const SlunkCryptEncr&); uint64_t m_nonce; slunkcrypt_t m_instance; }; class SlunkCryptDecr { public: SlunkCryptDecr(const uint64_t nonce, const std::string& passwd) { if ((m_instance = slunkcrypt_alloc(nonce, (const uint8_t*)passwd.c_str(), passwd.length())) == SLUNKCRYPT_NULL) { throw std::runtime_error("Failed to create encoder instance!"); } } SlunkCryptDecr(SlunkCryptDecr &&other) noexcept { this->m_instance = other.m_instance; other.m_instance = SLUNKCRYPT_NULL; } ~SlunkCryptDecr(void) { if (m_instance != SLUNKCRYPT_NULL) { slunkcrypt_free(m_instance); } } bool decrypt(const uint8_t* const input, uint8_t* const output, size_t length) { return (slunkcrypt_decrypt(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS); } bool decrypt(const std::vector& input, std::vector& output) { if (output.size() >= input.size()) { return (slunkcrypt_decrypt(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS); } return false; } bool decrypt_inplace(uint8_t* const buffer, size_t length) { return (slunkcrypt_decrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS); } bool decrypt_inplace(std::vector& buffer) { return (slunkcrypt_decrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS); } private: SlunkCryptDecr(const SlunkCryptDecr&); SlunkCryptDecr& operator=(const SlunkCryptDecr&); slunkcrypt_t m_instance; }; #endif