mirror of
https://github.com/matrix-construct/construct
synced 2024-05-29 16:23:45 +02:00
ircd: Split and reorg ircd::hash/ircd::crh related headers and units.
This commit is contained in:
parent
26d7e4e6de
commit
a7e4acc519
|
@ -9,20 +9,7 @@
|
||||||
// full license for this software is available in the LICENSE file.
|
// full license for this software is available in the LICENSE file.
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#define HAVE_IRCD_HASH_H
|
#define HAVE_IRCD_CRH_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 char16_t *const str, const size_t i = 0);
|
|
||||||
template<size_t PRIME = 7681> constexpr size_t hash(const 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::u16string &str, const size_t i = 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Collision-Resistant Hashing
|
/// Collision-Resistant Hashing
|
||||||
///
|
///
|
||||||
|
@ -40,7 +27,6 @@ namespace ircd::crh
|
||||||
// Export aliases down to ircd::
|
// Export aliases down to ircd::
|
||||||
namespace ircd
|
namespace ircd
|
||||||
{
|
{
|
||||||
using crh::hash;
|
|
||||||
using crh::sha256;
|
using crh::sha256;
|
||||||
using crh::ripemd160;
|
using crh::ripemd160;
|
||||||
}
|
}
|
||||||
|
@ -155,30 +141,3 @@ const
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 string_view. Non-cryptographic.
|
|
||||||
template<size_t PRIME>
|
|
||||||
constexpr size_t
|
|
||||||
ircd::hash(const 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];
|
|
||||||
}
|
|
|
@ -43,7 +43,7 @@ namespace ircd
|
||||||
#include "info.h"
|
#include "info.h"
|
||||||
#include "nacl.h"
|
#include "nacl.h"
|
||||||
#include "rand.h"
|
#include "rand.h"
|
||||||
#include "hash.h"
|
#include "crh.h"
|
||||||
#include "ed25519.h"
|
#include "ed25519.h"
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "lex_cast.h"
|
#include "lex_cast.h"
|
||||||
|
|
52
include/ircd/util/hash.h
Normal file
52
include/ircd/util/hash.h
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#define HAVE_IRCD_UTIL_HASH_H
|
||||||
|
|
||||||
|
namespace ircd::util
|
||||||
|
{
|
||||||
|
// 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 char16_t *const str, const size_t i = 0);
|
||||||
|
template<size_t PRIME = 7681> constexpr size_t hash(const 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::u16string &str, const size_t i = 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runtime hashing of a std::u16string (for js). Non-cryptographic.
|
||||||
|
template<size_t PRIME>
|
||||||
|
size_t
|
||||||
|
ircd::util::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 string_view. Non-cryptographic.
|
||||||
|
template<size_t PRIME>
|
||||||
|
constexpr size_t
|
||||||
|
ircd::util::hash(const 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::util::hash(const char16_t *const str,
|
||||||
|
const size_t i)
|
||||||
|
{
|
||||||
|
return !str[i]? PRIME : (hash(str, i+1) * 33ULL) ^ str[i];
|
||||||
|
}
|
|
@ -54,6 +54,7 @@ namespace ircd
|
||||||
#include "what.h"
|
#include "what.h"
|
||||||
#include "u2a.h"
|
#include "u2a.h"
|
||||||
#include "pretty.h"
|
#include "pretty.h"
|
||||||
|
#include "hash.h"
|
||||||
|
|
||||||
// Unsorted section
|
// Unsorted section
|
||||||
namespace ircd {
|
namespace ircd {
|
||||||
|
|
|
@ -102,7 +102,7 @@ libircd_la_SOURCES = \
|
||||||
conf.cc \
|
conf.cc \
|
||||||
rfc1459.cc \
|
rfc1459.cc \
|
||||||
rand.cc \
|
rand.cc \
|
||||||
hash.cc \
|
crh.cc \
|
||||||
base.cc \
|
base.cc \
|
||||||
parse.cc \
|
parse.cc \
|
||||||
openssl.cc \
|
openssl.cc \
|
||||||
|
|
|
@ -8,6 +8,14 @@
|
||||||
// copyright notice and this permission notice is present in all copies. The
|
// copyright notice and this permission notice is present in all copies. The
|
||||||
// full license for this software is available in the LICENSE file.
|
// full license for this software is available in the LICENSE file.
|
||||||
|
|
||||||
|
//
|
||||||
|
// This file anchors the abstract ircd::crh::hash vtable and default
|
||||||
|
// functionalities. Various implementations of crh::hash will be contained
|
||||||
|
// within other units where the third-party dependency which implements it
|
||||||
|
// is included (ex. openssl.cc). This is so we don't include and mix
|
||||||
|
// everything here just for hash functions.
|
||||||
|
//
|
||||||
|
|
||||||
ircd::crh::hash::~hash()
|
ircd::crh::hash::~hash()
|
||||||
noexcept
|
noexcept
|
||||||
{
|
{
|
Loading…
Reference in a new issue