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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

92.0
/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) { m_hash->update(input, length); }
53,732,074✔
17

18
void Truncated_Hash::final_result(uint8_t out[]) {
13,432,914✔
19
   BOTAN_ASSERT_NOMSG(m_hash->output_length() * 8 >= m_output_bits);
13,432,914✔
20

21
   const auto full_output = m_hash->final();
13,432,914✔
22

23
   // truncate output to a full number of bytes
24
   const auto bytes = output_length();
13,432,914✔
25
   std::copy_n(full_output.begin(), bytes, out);
26,865,828✔
26

27
   // mask the unwanted bits in the final byte
28
   const uint8_t bits_in_last_byte = ((m_output_bits - 1) % 8) + 1;
13,432,914✔
29
   const uint8_t bitmask = ~((1 << (8 - bits_in_last_byte)) - 1);
13,432,914✔
30

31
   out[bytes - 1] &= bitmask;
13,432,914✔
32
}
13,432,914✔
33

34
size_t Truncated_Hash::output_length() const { return (m_output_bits + 7) / 8; }
13,432,914✔
35

36
std::string Truncated_Hash::name() const { return fmt("Truncated({},{})", m_hash->name(), m_output_bits); }
21✔
37

38
std::unique_ptr<HashFunction> Truncated_Hash::new_object() const {
227✔
39
   return std::make_unique<Truncated_Hash>(m_hash->new_object(), m_output_bits);
227✔
40
}
41

42
std::unique_ptr<HashFunction> Truncated_Hash::copy_state() const {
×
43
   return std::make_unique<Truncated_Hash>(m_hash->copy_state(), m_output_bits);
×
44
}
45

46
void Truncated_Hash::clear() { m_hash->clear(); }
272✔
47

48
Truncated_Hash::Truncated_Hash(std::unique_ptr<HashFunction> hash, size_t bits) :
295✔
49
      m_hash(std::move(hash)), m_output_bits(bits) {
295✔
50
   BOTAN_ASSERT_NONNULL(m_hash);
295✔
51

52
   if(m_output_bits == 0) {
295✔
53
      throw Invalid_Argument("Truncating a hash to 0 does not make sense");
1✔
54
   }
55

56
   if(m_hash->output_length() * 8 < m_output_bits) {
294✔
57
      throw Invalid_Argument("Underlying hash function does not produce enough bytes for truncation");
1✔
58
   }
59
}
295✔
60

61
}
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