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

randombit / botan / 12732895979

12 Jan 2025 11:00AM UTC coverage: 91.237% (-0.003%) from 91.24%
12732895979

push

github

web-flow
Merge pull request #4525 from randombit/jack/entropy-is-optional

Let the entropy module be optional

93458 of 102434 relevant lines covered (91.24%)

11406941.45 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 "../tests/test_rng.h"  // FIXME
8
#include "cli.h"
9

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

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

18
namespace Botan_CLI {
19

20
#if defined(BOTAN_HAS_ENTROPY_SOURCE)
21

22
class Entropy final : public Command {
23
   public:
24
      Entropy() : Command("entropy --truncate-at=128 source") {}
4✔
25

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

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

30
      void go() override {
1✔
31
         const std::string req_source = get_arg("source");
1✔
32
         const size_t truncate_sample = get_arg_sz("truncate-at");
1✔
33

34
         auto& entropy_sources = Botan::Entropy_Sources::global_sources();
1✔
35

36
         std::vector<std::string> sources;
1✔
37
         if(req_source == "all") {
1✔
38
            sources = entropy_sources.enabled_sources();
1✔
39
         } else {
40
            sources.push_back(req_source);
×
41
         }
42

43
         for(const std::string& source : sources) {
5✔
44
            Botan_Tests::SeedCapturing_RNG rng;
4✔
45
            const size_t entropy_estimate = entropy_sources.poll_just(rng, source);
4✔
46

47
            if(rng.samples() == 0) {
4✔
48
               output() << "Source " << source << " is unavailable\n";
×
49
               continue;
×
50
            }
51

52
            const auto& sample = rng.seed_material();
4✔
53

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

57
   #if defined(BOTAN_HAS_COMPRESSION)
58
            if(!sample.empty()) {
4✔
59
               auto comp = Botan::Compression_Algorithm::create("zlib");
4✔
60
               if(comp) {
4✔
61
                  try {
4✔
62
                     Botan::secure_vector<uint8_t> compressed;
4✔
63
                     compressed.assign(sample.begin(), sample.end());
4✔
64
                     comp->start(9);
4✔
65
                     comp->finish(compressed);
4✔
66

67
                     if(compressed.size() < sample.size()) {
4✔
68
                        output() << "Sample from " << source << " was zlib compressed from " << sample.size()
×
69
                                 << " bytes to " << compressed.size() << " bytes\n";
×
70
                     }
71
                  } catch(std::exception& e) {
4✔
72
                     error_output() << "Error while attempting to compress: " << e.what() << "\n";
×
73
                  }
×
74
               }
75
            }
4✔
76
   #endif
77

78
            if(sample.size() <= truncate_sample) {
4✔
79
               output() << Botan::hex_encode(sample) << "\n";
2✔
80
            } else if(truncate_sample > 0) {
3✔
81
               output() << Botan::hex_encode(&sample[0], truncate_sample) << "...\n";
9✔
82
            }
83
         }
4✔
84
      }
1✔
85
};
86

87
BOTAN_REGISTER_COMMAND("entropy", Entropy);
2✔
88

89
#endif
90

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