#ifndef LLARP_ENCCRYPTED_HPP #define LLARP_ENCCRYPTED_HPP #include #include #include #include #include #include namespace llarp { /// encrypted buffer base type struct Encrypted { Encrypted(Encrypted&& other); Encrypted(const Encrypted& other); Encrypted(); 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, _buf, _sz); } bool operator==(const Encrypted& other) const { return _sz == other._sz && memcmp(_buf, other._buf, _sz) == 0; } bool operator!=(const Encrypted& other) const { return !(*this == other); } Encrypted& operator=(const Encrypted& other) { return (*this) = other.Buffer(); } Encrypted& operator=(const llarp_buffer_t& buf) { if(buf.sz && buf.sz <= sizeof(_buf)) { _sz = buf.sz; memcpy(_buf, buf.base, _sz); } UpdateBuffer(); return *this; } void Fill(byte_t fill) { size_t idx = 0; while(idx < _sz) _buf[idx++] = fill; } void Randomize() { if(_sz) randombytes(_buf, _sz); } bool BDecode(llarp_buffer_t* buf) { llarp_buffer_t strbuf; if(!bencode_read_string(buf, &strbuf)) return false; if(strbuf.sz == 0 || strbuf.sz > sizeof(_buf)) return false; _sz = strbuf.sz; memcpy(_buf, strbuf.base, _sz); UpdateBuffer(); return true; } llarp_buffer_t* Buffer() { return &m_Buffer; } llarp_buffer_t Buffer() const { return m_Buffer; } size_t size() { return _sz; } size_t size() const { return _sz; } byte_t* data() { return _buf; } const byte_t* data() const { return _buf; } protected: void UpdateBuffer() { m_Buffer.base = data(); m_Buffer.cur = data(); m_Buffer.sz = size(); } byte_t _buf[MAX_LINK_MSG_SIZE]; size_t _sz; llarp_buffer_t m_Buffer; }; } // namespace llarp #endif