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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

93.55
/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

14
namespace Botan {
15

16
void Truncated_Hash::add_data(const uint8_t input[], size_t length) {
53,747,712✔
17
   m_hash->update(input, length);
53,747,712✔
18
}
53,747,712✔
19

20
void Truncated_Hash::final_result(uint8_t out[]) {
13,436,822✔
21
   BOTAN_ASSERT_NOMSG(m_hash->output_length() * 8 >= m_output_bits);
13,436,822✔
22

23
   const auto full_output = m_hash->final();
13,436,822✔
24

25
   // truncate output to a full number of bytes
26
   const auto bytes = output_length();
13,436,822✔
27
   std::copy_n(full_output.begin(), bytes, out);
26,873,644✔
28

29
   // mask the unwanted bits in the final byte
30
   const uint8_t bits_in_last_byte = ((m_output_bits - 1) % 8) + 1;
13,436,822✔
31
   const uint8_t bitmask = ~((1 << (8 - bits_in_last_byte)) - 1);
13,436,822✔
32

33
   out[bytes - 1] &= bitmask;
13,436,822✔
34
}
13,436,822✔
35

36
size_t Truncated_Hash::output_length() const {
27,422,940✔
37
   return (m_output_bits + 7) / 8;
13,436,822✔
38
}
39

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

44
std::unique_ptr<HashFunction> Truncated_Hash::new_object() const {
227✔
45
   return std::make_unique<Truncated_Hash>(m_hash->new_object(), m_output_bits);
227✔
46
}
47

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

52
void Truncated_Hash::clear() {
275✔
53
   m_hash->clear();
275✔
54
}
275✔
55

56
Truncated_Hash::Truncated_Hash(std::unique_ptr<HashFunction> hash, size_t bits) :
295✔
57
      m_hash(std::move(hash)), m_output_bits(bits) {
295✔
58
   BOTAN_ASSERT_NONNULL(m_hash);
295✔
59

60
   if(m_output_bits == 0) {
295✔
61
      throw Invalid_Argument("Truncating a hash to 0 does not make sense");
1✔
62
   }
63

64
   if(m_hash->output_length() * 8 < m_output_bits) {
294✔
65
      throw Invalid_Argument("Underlying hash function does not produce enough bytes for truncation");
1✔
66
   }
67
}
295✔
68

69
}  // 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