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

randombit / botan / 23111636944

15 Mar 2026 01:45PM UTC coverage: 89.732% (+0.002%) from 89.73%
23111636944

Pull #5446

github

web-flow
Merge 75b20e42b into e9952d62f
Pull Request #5446: Address (or silence) various new warnings from clang-tidy 22

104217 of 116142 relevant lines covered (89.73%)

11426748.42 hits per line

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

79.63
/src/cli/entropy.cpp
1
/*
2
* (C) 2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#if defined(BOTAN_HAS_ENTROPY_SOURCE)
10
   #include <botan/entropy_src.h>
11
   #include <botan/hex.h>
12
   #include <botan/rng.h>
13
#endif
14

15
#if defined(BOTAN_HAS_COMPRESSION)
16
   #include <botan/compression.h>
17
#endif
18

19
namespace Botan_CLI {
20

21
namespace {
22

23
#if defined(BOTAN_HAS_ENTROPY_SOURCE)
24

25
class SeedCapturing_RNG final : public Botan::RandomNumberGenerator {
4✔
26
   public:
27
      bool accepts_input() const override { return true; }
2✔
28

29
      void clear() override {}
×
30

31
      bool is_seeded() const override { return false; }
×
32

33
      std::string name() const override { return "SeedCapturing"; }
×
34

35
      size_t samples() const { return m_samples; }
8✔
36

37
      const std::vector<uint8_t>& seed_material() const { return m_seed; }
38

39
   private:
40
      void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
4✔
41
         if(!output.empty()) {
4✔
42
            throw CLI_Error("SeedCapturing_RNG has no output");
×
43
         }
44

45
         m_samples++;
4✔
46
         m_seed.insert(m_seed.end(), input.begin(), input.end());
4✔
47
      }
4✔
48

49
   private:
50
      std::vector<uint8_t> m_seed;
51
      size_t m_samples = 0;
52
};
53

54
class Entropy final : public Command {
55
   public:
56
      Entropy() : Command("entropy --truncate-at=128 source") {}
4✔
57

58
      std::string group() const override { return "misc"; }
1✔
59

60
      std::string description() const override { return "Sample a raw entropy source"; }
1✔
61

62
      void go() override {
1✔
63
         const std::string req_source = get_arg("source");
1✔
64
         const size_t truncate_sample = get_arg_sz("truncate-at");
1✔
65

66
         auto& entropy_sources = Botan::Entropy_Sources::global_sources();
1✔
67

68
         std::vector<std::string> sources;
1✔
69
         if(req_source == "all") {
1✔
70
            sources = entropy_sources.enabled_sources();
1✔
71
         } else {
72
            sources.push_back(req_source);
×
73
         }
74

75
         for(const std::string& source : sources) {
5✔
76
            SeedCapturing_RNG rng;
4✔
77
            const size_t entropy_estimate = entropy_sources.poll_just(rng, source);
4✔
78

79
            if(rng.samples() == 0) {
4✔
80
               output() << "Source " << source << " is unavailable\n";
×
81
               continue;
×
82
            }
83

84
            const auto& sample = rng.seed_material();
4✔
85

86
            output() << "Polling " << source << " gathered " << sample.size() << " bytes in " << rng.samples()
4✔
87
                     << " outputs with estimated entropy " << entropy_estimate << "\n";
4✔
88

89
   #if defined(BOTAN_HAS_COMPRESSION)
90
            if(!sample.empty()) {
4✔
91
               auto comp = Botan::Compression_Algorithm::create("zlib");
4✔
92
               if(comp) {
4✔
93
                  try {
4✔
94
                     Botan::secure_vector<uint8_t> compressed;
4✔
95
                     compressed.assign(sample.begin(), sample.end());
4✔
96
                     comp->start(9);
4✔
97
                     comp->finish(compressed);
4✔
98

99
                     if(compressed.size() < sample.size()) {
4✔
100
                        output() << "Sample from " << source << " was zlib compressed from " << sample.size()
×
101
                                 << " bytes to " << compressed.size() << " bytes\n";
×
102
                     }
103
                  } catch(std::exception& e) {
4✔
104
                     error_output() << "Error while attempting to compress: " << e.what() << "\n";
×
105
                  }
×
106
               }
107
            }
4✔
108
   #endif
109

110
            if(sample.size() <= truncate_sample) {
4✔
111
               output() << Botan::hex_encode(sample) << "\n";
2✔
112
            } else if(truncate_sample > 0) {
3✔
113
               output() << Botan::hex_encode(sample.data(), truncate_sample) << "...\n";
9✔
114
            }
115
         }
4✔
116
      }
1✔
117
};
118

119
BOTAN_REGISTER_COMMAND("entropy", Entropy);
2✔
120

121
#endif
122

123
}  // namespace
124

125
}  // namespace Botan_CLI
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