136 lines
3.4 KiB
C++
136 lines
3.4 KiB
C++
|
/******************************************************************************/
|
||
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
||
|
/* This work has been released under the CC0 1.0 Universal license! */
|
||
|
/******************************************************************************/
|
||
|
|
||
|
#ifndef INC_SLUNKCRYPT_PLUSPLUS
|
||
|
#define INC_SLUNKCRYPT_PLUSPLUS
|
||
|
|
||
|
#include "slunkcrypt.h"
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
#include <stdexcept>
|
||
|
|
||
|
class SlunkCryptEncr
|
||
|
{
|
||
|
public:
|
||
|
SlunkCryptEncr(const std::string &passwd)
|
||
|
{
|
||
|
if (slunkcrypt_generate_salt(&m_salt) != SLUNKCRYPT_SUCCESS)
|
||
|
{
|
||
|
throw std::runtime_error("Failed to generate the seed value!");
|
||
|
}
|
||
|
if ((m_instance = slunkcrypt_alloc(m_salt, (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_salt = other.m_salt;
|
||
|
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<uint8_t> &input, std::vector<uint8_t> &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<uint8_t> &buffer)
|
||
|
{
|
||
|
return (slunkcrypt_encrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
|
||
|
}
|
||
|
|
||
|
uint64_t salt_value(void) const
|
||
|
{
|
||
|
return m_salt;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
SlunkCryptEncr(const SlunkCryptEncr&);
|
||
|
SlunkCryptEncr& operator=(const SlunkCryptEncr&);
|
||
|
uint64_t m_salt;
|
||
|
slunkcrypt_t m_instance;
|
||
|
};
|
||
|
|
||
|
class SlunkCryptDecr
|
||
|
{
|
||
|
public:
|
||
|
SlunkCryptDecr(const uint64_t salt, const std::string& passwd)
|
||
|
{
|
||
|
if ((m_instance = slunkcrypt_alloc(salt, (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<uint8_t>& input, std::vector<uint8_t>& 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<uint8_t>& 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
|