0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-10-31 02:48:58 +01:00
construct/include/ircd/hash.h

189 lines
5.4 KiB
C++

/*
* charybdis: 21st Century IRC++d
*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#pragma once
#define HAVE_IRCD_HASH_H
namespace ircd
{
// constexpr bernstein string hasher suite; these functions will hash the
// 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);
// Note that at runtime this hash uses multiplication on every character
// which can consume many cycles...
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);
}
/// Collision-Resistant Hashing
///
/// ircd::crh contains support for collision-resistant hash functions including
/// cryptographic hash functions.
namespace ircd::crh
{
IRCD_EXCEPTION(ircd::error, error)
struct hash;
struct sha256;
}
// Export aliases down to ircd::
namespace ircd
{
using crh::hash;
using crh::sha256;
}
/// Abstract interface to a hashing context for any algorithm in ircd::crh
///
/// Use this type when dealing with algorithm-agnostic hashing.
struct ircd::crh::hash
{
/// Returns the byte length of the mutable_raw_buffer for digests
virtual size_t length() const = 0;
/// Samples the digest at the current state (without modifying)
virtual void digest(const mutable_raw_buffer &) const = 0;
/// Samples the digest and modifies the state (depending on impl)
virtual void finalize(const mutable_raw_buffer &b)
{
digest(b);
}
/// Appends to the message
virtual void update(const const_raw_buffer &) = 0;
// conveniences for output
template<size_t SIZE> fixed_const_raw_buffer<SIZE> digest() const;
template<size_t SIZE> operator fixed_const_raw_buffer<SIZE>() const;
// conveniences for input
void operator()(const mutable_raw_buffer &out, const const_raw_buffer &in);
hash &operator+=(const const_raw_buffer &);
virtual ~hash() noexcept;
};
/// SHA-256 hashing device.
struct ircd::crh::sha256
:hash
{
struct ctx;
static constexpr const size_t digest_size
{
256 / 8
};
using buf = fixed_const_raw_buffer<digest_size>;
protected:
std::unique_ptr<ctx> ctx;
public:
size_t length() const override final;
void digest(const mutable_raw_buffer &) const override final;
void finalize(const mutable_raw_buffer &) override final;
void update(const const_raw_buffer &) override final;
sha256(const mutable_raw_buffer &, const const_raw_buffer &);
sha256(const const_raw_buffer &);
sha256();
~sha256() noexcept;
};
/// Automatic gratification from hash::digest()
template<size_t SIZE>
ircd::crh::hash::operator
fixed_const_raw_buffer<SIZE>()
const
{
return digest<SIZE>();
}
/// Digests the hash into the buffer of the specified SIZE and returns it
template<size_t SIZE>
ircd::fixed_const_raw_buffer<SIZE>
ircd::crh::hash::digest()
const
{
assert(SIZE >= length());
return fixed_const_raw_buffer<SIZE>
{
[this](const auto &buffer)
{
this->digest(buffer);
}
};
}
/// Runtime hashing of a std::u16string (for js). Non-cryptographic.
template<size_t PRIME>
size_t
ircd::hash(const std::u16string &str,
const size_t i)
{
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
}
/// Runtime hashing of a std::string. Non-cryptographic.
template<size_t PRIME>
size_t
ircd::hash(const std::string &str,
const size_t i)
{
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
}
/// Runtime hashing of a string_view. Non-cryptographic.
template<size_t PRIME>
constexpr size_t
ircd::hash(const std::string_view &str,
const size_t i)
{
return i >= str.size()? PRIME : (hash(str, i+1) * 33ULL) ^ str.at(i);
}
/// Compile-time hashing of a wider string literal (for js). Non-cryptographic.
template<size_t PRIME>
constexpr size_t
ircd::hash(const char16_t *const &str,
const size_t i)
{
return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i];
}
/// Compile-time hashing of a string literal. Non-cryptographic.
template<size_t PRIME>
constexpr size_t
ircd::hash(const char *const &str,
const size_t i)
{
return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i];
}