29 #include <boost/random/random_device.hpp> 30 #include <boost/random/uniform_int_distribution.hpp> 31 #include <boost/functional/hash.hpp> 51 using Arith = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
78 explicit FixedHash(
bytes const& _b,
ConstructFromHashType _t = FailIfDifferent) {
if (_b.size() == N)
memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N));
else { m_data.fill(0);
if (_t != FailIfDifferent) {
auto c = std::min<unsigned>(_b.size(), N);
for (
unsigned i = 0; i <
c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.size() - 1 - i : i]; } } }
81 explicit FixedHash(
bytesConstRef _b,
ConstructFromHashType _t = FailIfDifferent) {
if (_b.
size() == N)
memcpy(m_data.data(), _b.
data(), std::min<unsigned>(_b.
size(), N));
else { m_data.fill(0);
if (_t != FailIfDifferent) {
auto c = std::min<unsigned>(_b.
size(), N);
for (
unsigned i = 0; i <
c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.
size() - 1 - i : i]; } } }
90 operator Arith()
const {
return fromBigEndian<Arith>(m_data); }
93 explicit operator bool()
const {
return std::any_of(m_data.begin(), m_data.end(), [](
byte _b) {
return _b != 0; }); }
98 bool operator<(
FixedHash const& _c)
const {
for (
unsigned i = 0; i < N; ++i)
if (m_data[i] < _c.
m_data[i])
return true;
else if (m_data[i] > _c.
m_data[i])
return false;
return false; }
148 std::array<byte, N>&
asArray() {
return m_data; }
151 std::array<byte, N>
const&
asArray()
const {
return m_data; }
154 template <
class Engine>
157 for (
auto& i: m_data)
158 i = (uint8_t)boost::random::uniform_int_distribution<uint16_t>(0, 255)(_eng);
172 return (*
this |= _h.template bloomPart<P, N>());
177 return contains(_h.template bloomPart<P, N>());
182 unsigned const c_bloomBits = M * 8;
183 unsigned const c_mask = c_bloomBits - 1;
186 static_assert((M & (M - 1)) == 0,
"M must be power-of-two");
187 static_assert(
P * c_bloomBytes <= N,
"out of range");
191 for (
unsigned i = 0; i <
P; ++i)
194 for (
unsigned j = 0; j < c_bloomBytes; ++j, ++p)
195 index = (index << 8) | *p;
197 ret[M - 1 - index / 8] |= (1 << (index % 8));
208 for (;; ++ret,
d <<= 1)
223 template <
unsigned T>
304 const uint64_t* hash1 = (
const uint64_t*)
data();
305 const uint64_t* hash2 = (
const uint64_t*)_other.
data();
306 return (hash1[0] == hash2[0]) && (hash1[1] == hash2[1]) && (hash1[2] == hash2[2]) && (hash1[3] == hash2[3]);
312 uint64_t
const*
data =
reinterpret_cast<uint64_t const*
>(value.
data());
313 return boost::hash_range(data, data + 4);
317 template <
unsigned N>
318 inline std::ostream& operator<<(std::ostream& _out, FixedHash<N>
const& _h)
320 _out << std::noshowbase << std::hex << std::setfill(
'0');
321 for (
unsigned i = 0; i < N; ++i)
322 _out << std::setw(2) << (int)_h[i];
328 template <
unsigned N>
329 inline std::ostream& operator<<(std::ostream& _out, SecureFixedHash<N>
const& _h)
331 _out <<
"SecureFixedHash#" << std::hex << typename FixedHash<N>::hash()(_h.makeInsecure()) << std::dec;
374 std::ostringstream out;
377 out << i.abridged() <<
", ";
391 template<>
struct hash<dev::
h512>: dev::h512::hash {};
FixedHash operator^(FixedHash const &_c) const
h128 fromUUID(std::string const &_uuid)
bool operator>(SecureFixedHash const &_c) const
bool operator<(SecureFixedHash const &_c) const
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
bool operator<=(SecureFixedHash const &_c) const
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
bool operator<=(FixedHash const &_c) const
vector_ref< _T const > ref(_T const &_t)
bool operator==(SecureFixedHash const &_c) const
SecureFixedHash operator^(SecureFixedHash const &_c) const
FixedHash & operator|=(FixedHash const &_c)
byte const * data() const
void toBigEndian(T _val, Out &o_out)
Converts a templated integer value to the big-endian byte-stream represented on a templated collectio...
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
SecureFixedHash & operator&=(FixedHash< T > const &_c)
bool contains(FixedHash const &_c) const
FixedHash operator&(FixedHash const &_c) const
FixedHash operator~() const
FixedHash< M > bloomPart() const
FixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=AlignLeft)
Construct from another hash, filling with zeroes or cropping as necessary.
secure_vector< byte > bytesSec
FixedHash(unsigned _u)
Convert from unsigned.
SecureFixedHash & operator|=(FixedHash< T > const &_c)
std::hash for asio::adress
FixedHash operator|(FixedHash const &_c) const
byte & operator[](unsigned _i)
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
std::array< byte, N > const & asArray() const
Compile-time calculation of Log2 of constant values.
FixedHash & shiftBloom(FixedHash< M > const &_h)
FixedHash()
Construct an empty hash.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> Arith
The corresponding arithmetic type.
std::vector< h512 > h512s
bool operator!=(FixedHash const &_c) const
SecureFixedHash(bytesSec const &_b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
SecureFixedHash(std::string const &_s, ConstructFromStringType _t=FixedHash< T >::FromHex, ConstructFromHashType _ht=FixedHash< T >::FailIfDifferent)
bool operator>(FixedHash const &_c) const
void randomize(Engine &_eng)
Populate with random data.
bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
SecureFixedHash(bytes const &_b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
bool containsBloom(FixedHash< M > const &_h)
bool operator<(FixedHash const &_c) const
FixedHash(bytes const &_b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
bytesConstRef ref() const
bool operator==(FixedHash const &_c) const
bool operator<=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
FixedHash(std::string const &_s, ConstructFromStringType _t=FromHex, ConstructFromHashType _ht=FailIfDifferent)
Explicitly construct, copying from a string.
boost::random_device s_fixedHashEngine
FixedHash(byte const *_bs, ConstructFromPointerType)
Explicitly construct, copying from a bytes in memory with given pointer.
FixedHash(Arith const &_arith)
Convert from the corresponding arithmetic type.
static SecureFixedHash< T > random()
vector_ref< byte > bytesRef
bytesSec asBytesSec() const
FixedHash< T > const & makeInsecure() const
bool contains(T const &_t, V const &_v)
SecureFixedHash & operator|=(SecureFixedHash const &_c)
FixedHash(bytesConstRef _b, ConstructFromHashType _t=FailIfDifferent)
Explicitly construct, copying from a byte array.
SecureFixedHash(bytesConstRef _b, ConstructFromHashType _t=FixedHash< T >::FailIfDifferent)
std::unordered_set< h160 > h160Hash
std::vector< byte > bytes
vector_ref< byte const > bytesConstRef
SecureFixedHash & operator&=(SecureFixedHash const &_c)
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
SecureFixedHash operator|(FixedHash< T > const &_c) const
bool operator!=(SecureFixedHash const &_c) const
FixedHash< T > & writable()
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
SecureFixedHash & operator^=(FixedHash< T > const &_c)
SecureFixedHash operator|(SecureFixedHash const &_c) const
static FixedHash random()
bool operator>=(SecureFixedHash const &_c) const
byte operator[](unsigned _i) const
std::vector< h160 > h160s
h160 left160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the left 20 bytes.
SecureFixedHash(bytes const *_d, ConstructFromPointerType _t)
uint8_t const size_t const size
SecureFixedHash & operator^=(SecureFixedHash const &_c)
void * memcpy(void *a, const void *b, size_t c)
std::string abridgedMiddle() const
bool operator>=(FixedHash const &_c) const
size_t operator()(FixedHash const &_value) const
Make a hash of the object's data.
FixedHash & operator&=(FixedHash const &_c)
SecureFixedHash operator&(SecureFixedHash const &_c) const
SecureFixedHash operator&(FixedHash< T > const &_c) const
std::string abridged() const
std::unordered_set< h256 > h256Hash
SecureFixedHash operator~() const
ConstructFromHashType
Method to convert from a string.
std::array< byte, N > & asArray()
std::array< byte, N > m_data
The binary data.
byte const * data() const
std::vector< h256 > h256s
ConstructFromPointerType
A dummy flag to avoid accidental construction from pointer.
bytesConstRef ref() const
ConstructFromStringType
Method to convert from a string.
unsigned firstBitSet() const
Returns the index of the first bit set to one, or size() * 8 if no bits are set.
SecureFixedHash operator^(FixedHash< T > const &_c) const
FixedHash & operator^=(FixedHash const &_c)
SecureFixedHash(SecureFixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)
SecureFixedHash< T > & operator=(SecureFixedHash< T > const &_c)
std::string toUUID(h128 const &_uuid)
bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
SecureFixedHash(FixedHash< M > const &_h, ConstructFromHashType _t=FixedHash< T >::AlignLeft)