0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 10:12:39 +01:00

ircd::crh: Add sha1 hashing device.

This commit is contained in:
Jason Volk 2019-01-19 11:59:51 -08:00
parent 1041118b9b
commit 18091eeb46
2 changed files with 164 additions and 1 deletions

View file

@ -20,6 +20,7 @@ namespace ircd::crh
IRCD_EXCEPTION(ircd::error, error)
struct hash;
struct sha1;
struct sha256;
struct ripemd160;
}
@ -27,6 +28,7 @@ namespace ircd::crh
// Export aliases down to ircd::
namespace ircd
{
using crh::sha1;
using crh::sha256;
using crh::ripemd160;
}
@ -59,6 +61,35 @@ struct ircd::crh::hash
virtual ~hash() noexcept;
};
/// SHA-1 hashing device.
struct ircd::crh::sha1
final
:hash
{
struct ctx;
static constexpr const size_t &digest_size
{
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;
sha1(const mutable_buffer &, const const_buffer &); // note: finalizes
sha1(const const_buffer &); // note: finalizes
sha1();
~sha1() noexcept;
};
/// SHA-256 hashing device.
struct ircd::crh::sha256
final

View file

@ -1427,9 +1427,141 @@ ircd::openssl::init::~init()
///////////////////////////////////////////////////////////////////////////////
//
// hash.h
// crh.h
//
//
// sha1
//
namespace ircd::crh
{
static void finalize(struct sha1::ctx *const &, const mutable_buffer &);
}
struct ircd::crh::sha1::ctx
:SHA_CTX
{
static constexpr const size_t &MAX_CTXS {64};
static thread_local allocator::fixed<ctx, MAX_CTXS> ctxs;
static void *operator new(const size_t count);
static void operator delete(void *const ptr, const size_t count);
ctx();
~ctx() noexcept;
};
decltype(ircd::crh::sha1::ctx::ctxs)
thread_local ircd::crh::sha1::ctx::ctxs
{};
void *
ircd::crh::sha1::ctx::operator new(const size_t bytes)
{
assert(bytes > 0);
assert(bytes % sizeof(ctx) == 0);
return ctxs().allocate(bytes / sizeof(ctx));
}
void
ircd::crh::sha1::ctx::operator delete(void *const ptr,
const size_t bytes)
{
if(!ptr)
return;
assert(bytes % sizeof(ctx) == 0);
ctxs().deallocate(reinterpret_cast<ctx *>(ptr), bytes / sizeof(ctx));
}
//
// sha1::ctx::ctx
//
ircd::crh::sha1::ctx::ctx()
{
openssl::call(::SHA1_Init, this);
}
ircd::crh::sha1::ctx::~ctx()
noexcept
{
}
//
// sha1::sha1
//
ircd::crh::sha1::sha1()
:ctx{std::make_unique<struct ctx>()}
{
}
/// One-shot functor. Immediately calls update(); no output
ircd::crh::sha1::sha1(const const_buffer &in)
:sha1{}
{
update(in);
}
/// One-shot functor. Immediately calls operator(). NOTE: This hashing context
/// cannot be used again after this ctor.
ircd::crh::sha1::sha1(const mutable_buffer &out,
const const_buffer &in)
:sha1{}
{
operator()(out, in);
}
ircd::crh::sha1::~sha1()
noexcept
{
}
void
ircd::crh::sha1::update(const const_buffer &buf)
{
assert(bool(ctx));
openssl::call(::SHA1_Update, ctx.get(), data(buf), size(buf));
}
void
ircd::crh::sha1::digest(const mutable_buffer &buf)
const
{
assert(bool(ctx));
auto copy(*ctx);
crh::finalize(&copy, buf);
}
void
ircd::crh::sha1::finalize(const mutable_buffer &buf)
{
assert(bool(ctx));
crh::finalize(ctx.get(), buf);
}
size_t
ircd::crh::sha1::length()
const
{
return digest_size;
}
void
ircd::crh::finalize(struct sha1::ctx *const &ctx,
const mutable_buffer &buf)
{
assert(size(buf) >= sha1::digest_size);
uint8_t *const md
{
reinterpret_cast<uint8_t *>(data(buf))
};
openssl::call(::SHA1_Final, md, ctx);
}
//
// sha256
//