2020-04-07 04:01:43 -03:00
|
|
|
/*
|
2021-04-22 17:13:01 -03:00
|
|
|
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
2020-04-07 04:01:43 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-07 04:01:43 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
|
|
namespace Crypto {
|
2020-04-07 07:12:27 -03:00
|
|
|
namespace Hash {
|
2020-04-07 04:01:43 -03:00
|
|
|
|
2022-01-03 18:28:19 -03:00
|
|
|
template<size_t DigestS>
|
|
|
|
|
struct Digest {
|
|
|
|
|
static_assert(DigestS % 8 == 0);
|
|
|
|
|
constexpr static size_t Size = DigestS / 8;
|
|
|
|
|
u8 data[Size];
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE const u8* immutable_data() const { return data; }
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>>
|
2020-04-22 19:33:05 -03:00
|
|
|
class HashFunction {
|
|
|
|
|
public:
|
2022-01-03 18:28:19 -03:00
|
|
|
static_assert(BlockS % 8 == 0);
|
2020-04-22 19:33:05 -03:00
|
|
|
static constexpr auto BlockSize = BlockS / 8;
|
2022-01-03 18:28:19 -03:00
|
|
|
|
|
|
|
|
static_assert(DigestS % 8 == 0);
|
|
|
|
|
static constexpr auto DigestSize = DigestS / 8;
|
2020-04-07 04:01:43 -03:00
|
|
|
|
2020-04-22 19:33:05 -03:00
|
|
|
using DigestType = DigestT;
|
2020-04-07 04:01:43 -03:00
|
|
|
|
2022-01-03 18:27:02 -03:00
|
|
|
constexpr static size_t block_size() { return BlockSize; }
|
|
|
|
|
constexpr static size_t digest_size() { return DigestSize; }
|
2020-04-07 04:01:43 -03:00
|
|
|
|
2020-04-22 19:33:05 -03:00
|
|
|
virtual void update(const u8*, size_t) = 0;
|
2020-12-19 11:56:15 -03:00
|
|
|
|
2022-01-03 18:27:02 -03:00
|
|
|
void update(Bytes buffer) { update(buffer.data(), buffer.size()); }
|
|
|
|
|
void update(ReadonlyBytes buffer) { update(buffer.data(), buffer.size()); }
|
|
|
|
|
void update(const ByteBuffer& buffer) { update(buffer.data(), buffer.size()); }
|
|
|
|
|
void update(StringView string) { update((const u8*)string.characters_without_null_termination(), string.length()); }
|
2020-04-07 04:01:43 -03:00
|
|
|
|
2020-04-22 19:33:05 -03:00
|
|
|
virtual DigestType peek() = 0;
|
|
|
|
|
virtual DigestType digest() = 0;
|
2020-04-07 22:54:28 -03:00
|
|
|
|
2020-04-22 19:33:05 -03:00
|
|
|
virtual void reset() = 0;
|
2020-04-29 11:47:47 -03:00
|
|
|
|
2020-04-22 19:33:05 -03:00
|
|
|
virtual String class_name() const = 0;
|
2021-04-15 14:43:29 -03:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual ~HashFunction() = default;
|
2020-04-22 19:33:05 -03:00
|
|
|
};
|
2020-04-07 07:12:27 -03:00
|
|
|
}
|
2020-04-07 04:01:43 -03:00
|
|
|
}
|