Botan 3.4.0
Crypto and TLS for C&
trunc_hash.h
Go to the documentation of this file.
1/**
2 * Wrapper for truncated hashes
3 * (C) 2023 Jack Lloyd
4 * 2023 René Meusel - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_TRUNCATED_HASH_H_
10#define BOTAN_TRUNCATED_HASH_H_
11
12#include <botan/hash.h>
13
14namespace Botan {
15
16/**
17 * Wrapper class to truncate underlying hash function output to a given number
18 * of bits. The leading bits are retained. Since the HashFunction interface is
19 * defined to return bytes, if the desired truncation length is not a multiple
20 * of 8, then the final byte of the output will have some number of trailing
21 * bits always set to zero.
22 */
24 public:
25 void clear() override;
26 std::string name() const override;
27 std::unique_ptr<HashFunction> new_object() const override;
28 std::unique_ptr<HashFunction> copy_state() const override;
29
30 size_t output_length() const override;
31
32 /**
33 * @param hash the underlying hash function whose output shall be truncated
34 * @param length the number of bits the hash shall be truncated to
35 */
36 Truncated_Hash(std::unique_ptr<HashFunction> hash, size_t length);
37
38 private:
39 void add_data(std::span<const uint8_t>) override;
40 void final_result(std::span<uint8_t>) override;
41
42 std::unique_ptr<HashFunction> m_hash;
43 size_t m_output_bits;
44
46};
47
48} // namespace Botan
49
50#endif
Truncated_Hash(std::unique_ptr< HashFunction > hash, size_t length)
size_t output_length() const override
std::string name() const override
std::unique_ptr< HashFunction > new_object() const override
std::unique_ptr< HashFunction > copy_state() const override
void clear() override
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61