• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13219613273

08 Feb 2025 08:36PM UTC coverage: 91.657% (+0.003%) from 91.654%
13219613273

push

github

web-flow
Merge pull request #4650 from randombit/jack/header-minimization

Reorganize code and reduce header dependencies

94838 of 103471 relevant lines covered (91.66%)

11212567.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/lib/hash/trunc_hash/trunc_hash.cpp
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
#include <botan/internal/trunc_hash.h>
10

11
#include <botan/exceptn.h>
12
#include <botan/internal/fmt.h>
13
#include <algorithm>
14

15
namespace Botan {
16

17
void Truncated_Hash::add_data(std::span<const uint8_t> input) {
230,690,746✔
18
   m_hash->update(input);
230,690,746✔
19
}
230,690,746✔
20

21
void Truncated_Hash::final_result(std::span<uint8_t> out) {
71,802,208✔
22
   BOTAN_ASSERT_NOMSG(m_hash->output_length() * 8 >= m_output_bits);
71,802,208✔
23

24
   m_hash->final(m_buffer);
71,802,208✔
25

26
   // truncate output to a full number of bytes
27
   const auto bytes = output_length();
71,802,208✔
28
   std::copy_n(m_buffer.begin(), bytes, out.data());
71,802,208✔
29
   zeroise(m_buffer);
71,802,208✔
30

31
   // mask the unwanted bits in the final byte
32
   const uint8_t bits_in_last_byte = ((m_output_bits - 1) % 8) + 1;
71,802,208✔
33
   const uint8_t bitmask = ~((1 << (8 - bits_in_last_byte)) - 1);
71,802,208✔
34

35
   out.back() &= bitmask;
71,802,208✔
36
}
71,802,208✔
37

38
size_t Truncated_Hash::output_length() const {
144,277,797✔
39
   return (m_output_bits + 7) / 8;
71,802,208✔
40
}
41

42
std::string Truncated_Hash::name() const {
21✔
43
   return fmt("Truncated({},{})", m_hash->name(), m_output_bits);
21✔
44
}
45

46
std::unique_ptr<HashFunction> Truncated_Hash::new_object() const {
763✔
47
   return std::make_unique<Truncated_Hash>(m_hash->new_object(), m_output_bits);
763✔
48
}
49

50
std::unique_ptr<HashFunction> Truncated_Hash::copy_state() const {
×
51
   return std::make_unique<Truncated_Hash>(m_hash->copy_state(), m_output_bits);
×
52
}
53

54
void Truncated_Hash::clear() {
276✔
55
   m_hash->clear();
276✔
56
}
276✔
57

58
Truncated_Hash::Truncated_Hash(std::unique_ptr<HashFunction> hash, size_t bits) :
13,429✔
59
      m_hash(std::move(hash)), m_output_bits(bits), m_buffer(m_hash->output_length()) {
13,429✔
60
   BOTAN_ASSERT_NONNULL(m_hash);
13,429✔
61

62
   if(m_output_bits == 0) {
13,429✔
63
      throw Invalid_Argument("Truncating a hash to 0 does not make sense");
1✔
64
   }
65

66
   if(m_hash->output_length() * 8 < m_output_bits) {
13,428✔
67
      throw Invalid_Argument("Underlying hash function does not produce enough bytes for truncation");
1✔
68
   }
69
}
13,431✔
70

71
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc