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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

88.1
/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
#include <botan/entropy_src.h>
11

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

16
namespace Botan_CLI {
17

18
class Entropy final : public Command {
19
   public:
20
      Entropy() : Command("entropy --truncate-at=128 source") {}
4✔
21

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

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

26
      void go() override {
1✔
27
         const std::string req_source = get_arg("source");
1✔
28
         const size_t truncate_sample = get_arg_sz("truncate-at");
1✔
29

30
         auto& entropy_sources = Botan::Entropy_Sources::global_sources();
1✔
31

32
         std::vector<std::string> sources;
1✔
33
         if(req_source == "all")
1✔
34
            sources = entropy_sources.enabled_sources();
1✔
35
         else
36
            sources.push_back(req_source);
×
37

38
         for(const std::string& source : sources) {
5✔
39
            Botan_Tests::SeedCapturing_RNG rng;
4✔
40
            const size_t entropy_estimate = entropy_sources.poll_just(rng, source);
4✔
41

42
            if(rng.samples() == 0) {
4✔
43
               output() << "Source " << source << " is unavailable\n";
1✔
44
               continue;
1✔
45
            }
46

47
            const auto& sample = rng.seed_material();
3✔
48

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

52
#if defined(BOTAN_HAS_COMPRESSION)
53
            if(!sample.empty()) {
3✔
54
               auto comp = Botan::Compression_Algorithm::create("zlib");
3✔
55
               if(comp) {
3✔
56
                  try {
3✔
57
                     Botan::secure_vector<uint8_t> compressed;
3✔
58
                     compressed.assign(sample.begin(), sample.end());
3✔
59
                     comp->start(9);
3✔
60
                     comp->finish(compressed);
3✔
61

62
                     if(compressed.size() < sample.size()) {
3✔
63
                        output() << "Sample from " << source << " was zlib compressed from " << sample.size()
×
64
                                 << " bytes to " << compressed.size() << " bytes\n";
×
65
                     }
66
                  } catch(std::exception& e) {
3✔
67
                     error_output() << "Error while attempting to compress: " << e.what() << "\n";
×
68
                  }
×
69
               }
70
            }
3✔
71
#endif
72

73
            if(sample.size() <= truncate_sample) {
3✔
74
               output() << Botan::hex_encode(sample) << "\n";
2✔
75
            } else if(truncate_sample > 0) {
2✔
76
               output() << Botan::hex_encode(&sample[0], truncate_sample) << "...\n";
6✔
77
            }
78
         }
4✔
79
      }
1✔
80
};
81

82
BOTAN_REGISTER_COMMAND("entropy", Entropy);
2✔
83

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

© 2025 Coveralls, Inc