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

randombit / botan / 22005812707

13 Feb 2026 11:02PM UTC coverage: 90.065%. Remained the same
22005812707

Pull #5327

github

web-flow
Merge 7932dd0e0 into bb74c40eb
Pull Request #5327: Various cleanups for tests.h

102225 of 113501 relevant lines covered (90.07%)

11587976.03 hits per line

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

83.33
/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
#if defined(BOTAN_HAS_ENTROPY_SOURCE)
22

23
namespace {
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; }
7✔
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 {
3✔
41
         if(!output.empty()) {
3✔
42
            throw CLI_Error("SeedCapturing_RNG has no output");
×
43
         }
44

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

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

54
}  // namespace
55

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

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

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

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

68
         auto& entropy_sources = Botan::Entropy_Sources::global_sources();
1✔
69

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

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

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

86
            const auto& sample = rng.seed_material();
3✔
87

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

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

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

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

121
BOTAN_REGISTER_COMMAND("entropy", Entropy);
2✔
122

123
#endif
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