9#include <botan/internal/trunc_hash.h>
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
16void Truncated_Hash::add_data(std::span<const uint8_t> input) {
17 m_hash->update(input);
20void Truncated_Hash::final_result(std::span<uint8_t> out) {
23 m_hash->final(m_buffer);
27 std::copy_n(m_buffer.begin(), bytes, out.data());
31 const uint8_t bits_in_last_byte = ((m_output_bits - 1) % 8) + 1;
32 const uint8_t bitmask = ~((1 << (8 - bits_in_last_byte)) - 1);
34 out.back() &= bitmask;
38 return (m_output_bits + 7) / 8;
42 return fmt(
"Truncated({},{})", m_hash->name(), m_output_bits);
46 return std::make_unique<Truncated_Hash>(m_hash->new_object(), m_output_bits);
50 return std::make_unique<Truncated_Hash>(m_hash->copy_state(), m_output_bits);
58 m_hash(std::move(hash)), m_output_bits(bits), m_buffer(m_hash->output_length()) {
61 if(m_output_bits == 0) {
65 if(m_hash->output_length() * 8 < m_output_bits) {
66 throw Invalid_Argument(
"Underlying hash function does not produce enough bytes for truncation");
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
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 zeroise(std::vector< T, Alloc > &vec)
std::string fmt(std::string_view format, const T &... args)