StormByte-Crypto 3.1.0
C++26 cryptography module of the StormByte suite
Loading...
Searching...
No Matches
generic.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Crypto.
5 *
6 * StormByte-Crypto original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Crypto source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, in particular Crypto++, bundled libbzip2, the StormByte-Buffer
23 * tree and the StormByte suite it vendors), which remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-Crypto is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-Crypto. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
43#include <StormByte/buffer/consumer.hxx>
46#include <StormByte/safe/binary.hxx>
47#include <StormByte/safe/clonable.hxx>
48#include <StormByte/type_traits.hxx>
49#include <StormByte/type_traits/safe.hxx>
50
51#include <span>
52
57namespace StormByte {
62 namespace Crypto {
67 namespace Hasher {
80
93 public:
102 Generic(const Generic& other) = default;
103
108 Generic(Generic&& other) noexcept = default;
109
113 virtual ~Generic() noexcept;
114
120 Generic& operator=(const Generic& other) = default;
121
127 Generic& operator=(Generic&& other) noexcept = default;
129
140 inline bool Hash(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
141 return DoHash(input, output);
142 }
143
151 template<StormByte::Type::ByteInputRange Range>
152 bool Hash(const Range& input, Buffer::WriteOnly& output) const {
153 Safe::Binary data;
154 for (const auto value: input)
155 data.push_back(static_cast<std::byte>(value));
156 return Hash(std::span<const std::byte>{data.span()}, output);
157 }
158
165 inline bool Hash(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
166 return DoHash(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
167 }
168
175 inline bool Hash(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
176 return DoHash(input, output, ReadMode::Move);
177 }
178
185 inline Buffer::Consumer Hash(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
186 return DoHash(consumer, mode);
187 }
188
193 inline enum Type Type() const noexcept {
194 return m_type;
195 }
196
197
198 protected:
200
205 inline Generic(enum Type type):
206 m_type(type) {}
207
208 private:
216 bool DoHash(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
217
224 virtual bool DoHash(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
225
232 virtual Buffer::Consumer DoHash(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
233 };
234
241 }
242 }
243}
244
BLAKE2b hasher.
Definition blake2b.hxx:66
BLAKE2s hasher.
Definition blake2s.hxx:66
Abstract hasher.
Definition generic.hxx:92
bool Hash(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Hash a read-only buffer (copy).
Definition generic.hxx:165
bool Hash(const Range &input, Buffer::WriteOnly &output) const
Hash an input range of byte-convertible values.
Definition generic.hxx:152
Generic(Generic &&other) noexcept=default
Move constructor.
enum Type m_type
Algorithm.
Definition generic.hxx:199
virtual ~Generic() noexcept
Destructor.
bool Hash(std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Hash a byte span into an output buffer.
Definition generic.hxx:140
enum Type Type() const noexcept
Algorithm of this hasher.
Definition generic.hxx:193
Buffer::Consumer Hash(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Hash a Consumer into another Consumer.
Definition generic.hxx:185
bool Hash(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Hash a buffer, consuming it.
Definition generic.hxx:175
Generic(enum Type type)
Construct with an algorithm.
Definition generic.hxx:205
Generic(const Generic &other)=default
Copy constructor.
SHA-256 hasher.
Definition sha256.hxx:66
SHA3-256 hasher.
Definition sha3_256.hxx:66
SHA3-512 hasher.
Definition sha3_512.hxx:66
SHA-512 hasher.
Definition sha512.hxx:66
STORMBYTE_FORCE_INLINE std::span< std::byte > span() noexcept
void push_back(std::byte value)
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Hash algorithms of the Crypto module.
Generic::PointerType Create(Type type) noexcept
Factory for a hasher.
Crypto module of the StormByte suite.
Definition aead.hxx:53
ReadMode
How a source buffer is consumed.
Definition typedefs.hxx:57
@ Copy
Copy the input.
Definition typedefs.hxx:58
@ Move
Consume the input.
Definition typedefs.hxx:59
Root namespace of the StormByte suite.
STL namespace.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53