2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-22 21:35:01 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_HASH_H
|
|
|
|
|
2017-09-30 08:09:34 +02:00
|
|
|
namespace ircd
|
2017-09-22 21:35:01 +02:00
|
|
|
{
|
2017-10-05 01:23:51 +02:00
|
|
|
// constexpr bernstein string hasher suite; these functions will hash the
|
2017-11-06 21:20:30 +01:00
|
|
|
// string at compile time leaving an integer residue at runtime. Decent
|
|
|
|
// primes are at least 7681 and 5381.
|
|
|
|
template<size_t PRIME = 7681> constexpr size_t hash(const char *const &str, const size_t i = 0);
|
|
|
|
template<size_t PRIME = 7681> constexpr size_t hash(const char16_t *const &str, const size_t i = 0);
|
|
|
|
template<size_t PRIME = 7681> constexpr size_t hash(const std::string_view &str, const size_t i = 0);
|
2017-09-22 21:35:01 +02:00
|
|
|
|
2017-10-05 01:23:51 +02:00
|
|
|
// Note that at runtime this hash uses multiplication on every character
|
|
|
|
// which can consume many cycles...
|
2017-11-06 21:20:30 +01:00
|
|
|
template<size_t PRIME = 7681> size_t hash(const std::string &str, const size_t i = 0);
|
|
|
|
template<size_t PRIME = 7681> size_t hash(const std::u16string &str, const size_t i = 0);
|
2017-09-22 21:35:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 01:23:51 +02:00
|
|
|
/// Collision-Resistant Hashing
|
|
|
|
///
|
|
|
|
/// ircd::crh contains support for collision-resistant hash functions including
|
|
|
|
/// cryptographic hash functions.
|
2017-09-23 01:29:06 +02:00
|
|
|
namespace ircd::crh
|
2017-09-22 21:35:01 +02:00
|
|
|
{
|
2017-09-23 01:29:06 +02:00
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
|
|
|
|
struct hash;
|
|
|
|
struct sha256;
|
2018-04-08 18:27:10 +02:00
|
|
|
struct ripemd160;
|
2017-09-23 01:29:06 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 01:23:51 +02:00
|
|
|
// Export aliases down to ircd::
|
2017-09-23 01:29:06 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
using crh::hash;
|
|
|
|
using crh::sha256;
|
2018-04-08 18:27:10 +02:00
|
|
|
using crh::ripemd160;
|
2017-09-23 01:29:06 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 01:23:51 +02:00
|
|
|
/// Abstract interface to a hashing context for any algorithm in ircd::crh
|
|
|
|
///
|
|
|
|
/// Use this type when dealing with algorithm-agnostic hashing.
|
2017-09-23 01:29:06 +02:00
|
|
|
struct ircd::crh::hash
|
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
/// Returns the byte length of the mutable_buffer for digests
|
2017-09-23 01:29:06 +02:00
|
|
|
virtual size_t length() const = 0;
|
2017-10-06 03:33:46 +02:00
|
|
|
|
|
|
|
/// Samples the digest at the current state (without modifying)
|
2018-02-03 08:20:26 +01:00
|
|
|
virtual void digest(const mutable_buffer &) const = 0;
|
2017-10-06 03:33:46 +02:00
|
|
|
|
|
|
|
/// Samples the digest and modifies the state (depending on impl)
|
2018-04-08 18:29:44 +02:00
|
|
|
virtual void finalize(const mutable_buffer &b);
|
2017-10-06 03:33:46 +02:00
|
|
|
|
|
|
|
/// Appends to the message
|
2018-02-03 08:20:26 +01:00
|
|
|
virtual void update(const const_buffer &) = 0;
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-10-06 03:33:46 +02:00
|
|
|
// conveniences for output
|
2018-02-03 08:20:26 +01:00
|
|
|
template<size_t SIZE> fixed_const_buffer<SIZE> digest() const;
|
|
|
|
template<size_t SIZE> operator fixed_const_buffer<SIZE>() const;
|
2017-10-06 03:33:46 +02:00
|
|
|
|
|
|
|
// conveniences for input
|
2018-02-03 08:20:26 +01:00
|
|
|
void operator()(const mutable_buffer &out, const const_buffer &in);
|
|
|
|
hash &operator+=(const const_buffer &);
|
2017-09-23 01:29:06 +02:00
|
|
|
|
|
|
|
virtual ~hash() noexcept;
|
|
|
|
};
|
|
|
|
|
2017-10-06 03:33:46 +02:00
|
|
|
/// SHA-256 hashing device.
|
2017-09-23 01:29:06 +02:00
|
|
|
struct ircd::crh::sha256
|
2018-01-27 06:58:52 +01:00
|
|
|
final
|
2017-09-23 01:29:06 +02:00
|
|
|
:hash
|
|
|
|
{
|
|
|
|
struct ctx;
|
|
|
|
|
2018-05-11 06:01:23 +02:00
|
|
|
static constexpr const size_t &digest_size
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
|
|
|
256 / 8
|
|
|
|
};
|
|
|
|
|
2018-02-03 08:20:26 +01:00
|
|
|
using buf = fixed_const_buffer<digest_size>;
|
2017-10-12 02:55:07 +02:00
|
|
|
|
2017-09-23 01:29:06 +02:00
|
|
|
protected:
|
|
|
|
std::unique_ptr<ctx> ctx;
|
|
|
|
|
|
|
|
public:
|
2018-01-27 06:58:52 +01:00
|
|
|
size_t length() const override;
|
2018-02-03 08:20:26 +01:00
|
|
|
void digest(const mutable_buffer &) const override;
|
|
|
|
void finalize(const mutable_buffer &) override;
|
|
|
|
void update(const const_buffer &) override;
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2018-04-08 18:45:47 +02:00
|
|
|
sha256(const mutable_buffer &, const const_buffer &); // note: finalizes
|
|
|
|
sha256(const const_buffer &); // note: finalizes
|
2017-09-23 01:29:06 +02:00
|
|
|
sha256();
|
|
|
|
~sha256() noexcept;
|
|
|
|
};
|
|
|
|
|
2018-04-08 18:27:10 +02:00
|
|
|
/// RIPEMD160 hashing device.
|
|
|
|
struct ircd::crh::ripemd160
|
|
|
|
final
|
|
|
|
:hash
|
|
|
|
{
|
|
|
|
struct ctx;
|
|
|
|
|
2018-05-11 06:01:23 +02:00
|
|
|
static constexpr const size_t &digest_size
|
2018-04-08 18:27:10 +02:00
|
|
|
{
|
|
|
|
160 / 8
|
|
|
|
};
|
|
|
|
|
|
|
|
using buf = fixed_const_buffer<digest_size>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::unique_ptr<ctx> ctx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
size_t length() const override;
|
|
|
|
void digest(const mutable_buffer &) const override;
|
|
|
|
void finalize(const mutable_buffer &) override;
|
|
|
|
void update(const const_buffer &) override;
|
|
|
|
|
2018-04-08 18:45:47 +02:00
|
|
|
ripemd160(const mutable_buffer &, const const_buffer &); // note: finalizes
|
|
|
|
ripemd160(const const_buffer &); // note: finalizes
|
2018-04-08 18:27:10 +02:00
|
|
|
ripemd160();
|
|
|
|
~ripemd160() noexcept;
|
|
|
|
};
|
|
|
|
|
2017-10-06 03:33:46 +02:00
|
|
|
/// Automatic gratification from hash::digest()
|
|
|
|
template<size_t SIZE>
|
|
|
|
ircd::crh::hash::operator
|
2018-02-03 08:20:26 +01:00
|
|
|
fixed_const_buffer<SIZE>()
|
2017-10-06 03:33:46 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return digest<SIZE>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Digests the hash into the buffer of the specified SIZE and returns it
|
|
|
|
template<size_t SIZE>
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::fixed_const_buffer<SIZE>
|
2017-10-06 03:33:46 +02:00
|
|
|
ircd::crh::hash::digest()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(SIZE >= length());
|
2018-02-03 08:20:26 +01:00
|
|
|
return fixed_const_buffer<SIZE>
|
2017-10-06 03:33:46 +02:00
|
|
|
{
|
|
|
|
[this](const auto &buffer)
|
|
|
|
{
|
|
|
|
this->digest(buffer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:55:07 +02:00
|
|
|
/// Runtime hashing of a std::u16string (for js). Non-cryptographic.
|
2017-11-06 21:20:30 +01:00
|
|
|
template<size_t PRIME>
|
|
|
|
size_t
|
2017-10-12 02:55:07 +02:00
|
|
|
ircd::hash(const std::u16string &str,
|
2017-09-30 08:09:34 +02:00
|
|
|
const size_t i)
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-11-06 21:20:30 +01:00
|
|
|
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
2017-09-22 21:35:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:55:07 +02:00
|
|
|
/// Runtime hashing of a std::string. Non-cryptographic.
|
2017-11-06 21:20:30 +01:00
|
|
|
template<size_t PRIME>
|
|
|
|
size_t
|
2017-10-12 02:55:07 +02:00
|
|
|
ircd::hash(const std::string &str,
|
2017-09-30 08:09:34 +02:00
|
|
|
const size_t i)
|
2017-09-22 21:35:01 +02:00
|
|
|
{
|
2017-11-06 21:20:30 +01:00
|
|
|
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
2017-09-22 21:35:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:55:07 +02:00
|
|
|
/// Runtime hashing of a string_view. Non-cryptographic.
|
2017-11-06 21:20:30 +01:00
|
|
|
template<size_t PRIME>
|
2017-10-12 02:55:07 +02:00
|
|
|
constexpr size_t
|
|
|
|
ircd::hash(const std::string_view &str,
|
2017-09-30 08:09:34 +02:00
|
|
|
const size_t i)
|
2017-09-22 21:35:01 +02:00
|
|
|
{
|
2017-11-06 21:20:30 +01:00
|
|
|
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
|
2017-09-22 21:35:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 03:33:46 +02:00
|
|
|
/// Compile-time hashing of a wider string literal (for js). Non-cryptographic.
|
2017-11-06 21:20:30 +01:00
|
|
|
template<size_t PRIME>
|
2017-09-22 21:35:01 +02:00
|
|
|
constexpr size_t
|
2017-09-30 08:09:34 +02:00
|
|
|
ircd::hash(const char16_t *const &str,
|
|
|
|
const size_t i)
|
2017-09-22 21:35:01 +02:00
|
|
|
{
|
2017-11-06 21:20:30 +01:00
|
|
|
return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i];
|
2017-09-22 21:35:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 03:33:46 +02:00
|
|
|
/// Compile-time hashing of a string literal. Non-cryptographic.
|
2017-11-06 21:20:30 +01:00
|
|
|
template<size_t PRIME>
|
2017-09-23 01:29:06 +02:00
|
|
|
constexpr size_t
|
2017-09-30 08:09:34 +02:00
|
|
|
ircd::hash(const char *const &str,
|
|
|
|
const size_t i)
|
2017-09-22 21:35:01 +02:00
|
|
|
{
|
2017-11-06 21:20:30 +01:00
|
|
|
return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i];
|
2017-09-22 21:35:01 +02:00
|
|
|
}
|