mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
1.9 KiB
96 lines
1.9 KiB
8 years ago
|
#ifndef TAG_H__
|
||
|
#define TAG_H__
|
||
|
|
||
9 years ago
|
/*
|
||
|
* Copyright (c) 2013-2016, The PurpleI2P Project
|
||
|
*
|
||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||
|
*
|
||
|
* See full license text in LICENSE file at top of project tree
|
||
|
*/
|
||
|
|
||
8 years ago
|
#include <boost/static_assert.hpp>
|
||
|
#include <string.h>
|
||
8 years ago
|
#include <openssl/rand.h>
|
||
9 years ago
|
#include "Base.h"
|
||
|
|
||
|
namespace i2p {
|
||
|
namespace data {
|
||
|
|
||
8 years ago
|
template<size_t sz>
|
||
|
class Tag
|
||
|
{
|
||
|
BOOST_STATIC_ASSERT_MSG(sz % 8 == 0, "Tag size must be multiple of 8 bytes");
|
||
9 years ago
|
|
||
8 years ago
|
public:
|
||
9 years ago
|
|
||
8 years ago
|
Tag () = default;
|
||
|
Tag (const uint8_t * buf) { memcpy (m_Buf, buf, sz); }
|
||
9 years ago
|
|
||
8 years ago
|
bool operator== (const Tag& other) const { return !memcmp (m_Buf, other.m_Buf, sz); }
|
||
|
bool operator< (const Tag& other) const { return memcmp (m_Buf, other.m_Buf, sz) < 0; }
|
||
9 years ago
|
|
||
8 years ago
|
uint8_t * operator()() { return m_Buf; }
|
||
|
const uint8_t * operator()() const { return m_Buf; }
|
||
9 years ago
|
|
||
8 years ago
|
operator uint8_t * () { return m_Buf; }
|
||
|
operator const uint8_t * () const { return m_Buf; }
|
||
9 years ago
|
|
||
8 years ago
|
const uint8_t * data() const { return m_Buf; }
|
||
|
const uint64_t * GetLL () const { return ll; }
|
||
8 years ago
|
|
||
8 years ago
|
bool IsZero () const
|
||
|
{
|
||
|
for (size_t i = 0; i < sz/8; ++i)
|
||
|
if (ll[i]) return false;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
8 years ago
|
void Fill(uint8_t c)
|
||
|
{
|
||
|
memset(m_Buf, c, sz);
|
||
|
}
|
||
8 years ago
|
|
||
|
void Randomize()
|
||
|
{
|
||
|
RAND_bytes(m_Buf, sz);
|
||
|
}
|
||
|
|
||
8 years ago
|
std::string ToBase64 () const
|
||
|
{
|
||
|
char str[sz*2];
|
||
|
size_t l = i2p::data::ByteStreamToBase64 (m_Buf, sz, str, sz*2);
|
||
|
return std::string (str, str + l);
|
||
|
}
|
||
|
|
||
|
std::string ToBase32 () const
|
||
|
{
|
||
|
char str[sz*2];
|
||
|
size_t l = i2p::data::ByteStreamToBase32 (m_Buf, sz, str, sz*2);
|
||
|
return std::string (str, str + l);
|
||
|
}
|
||
|
|
||
|
void FromBase32 (const std::string& s)
|
||
|
{
|
||
|
i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz);
|
||
|
}
|
||
|
|
||
|
void FromBase64 (const std::string& s)
|
||
|
{
|
||
|
i2p::data::Base64ToByteStream (s.c_str (), s.length (), m_Buf, sz);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
|
union // 8 bytes aligned
|
||
|
{
|
||
|
uint8_t m_Buf[sz];
|
||
|
uint64_t ll[sz/8];
|
||
9 years ago
|
};
|
||
8 years ago
|
};
|
||
|
|
||
9 years ago
|
} // data
|
||
|
} // i2p
|
||
|
|
||
|
#endif /* TAG_H__ */
|