lokinet/include/llarp/encrypted.hpp

61 lines
1.0 KiB
C++
Raw Normal View History

2018-06-11 13:25:10 +00:00
#ifndef LLARP_ENCCRYPTED_HPP
#define LLARP_ENCCRYPTED_HPP
#include <llarp/bencode.h>
2018-06-11 13:25:10 +00:00
#include <llarp/buffer.h>
2018-06-12 16:45:12 +00:00
#include <sodium.h>
2018-06-11 13:25:10 +00:00
namespace llarp
{
/// encrypted buffer base type
struct Encrypted
{
Encrypted() = default;
Encrypted(const byte_t* buf, size_t sz);
Encrypted(size_t sz);
~Encrypted();
bool
BEncode(llarp_buffer_t* buf) const
{
return bencode_write_bytestring(buf, data, size);
}
2018-06-12 16:45:12 +00:00
void
Randomize()
{
if(data)
randombytes(data, size);
}
bool
BDecode(llarp_buffer_t* buf)
{
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))
return false;
if(strbuf.sz == 0)
return false;
if(data)
delete[] data;
size = strbuf.sz;
data = new byte_t[size];
memcpy(data, strbuf.base, size);
return true;
}
2018-06-11 13:25:10 +00:00
llarp_buffer_t*
Buffer()
{
return &m_Buffer;
}
byte_t* data = nullptr;
size_t size = 0;
private:
llarp_buffer_t m_Buffer;
};
}
#endif