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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

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

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

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

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

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

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

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

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

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