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

randombit / botan / 4837926998

29 Apr 2023 09:37AM UTC coverage: 92.145% (+0.03%) from 92.117%
4837926998

push

github

77595 of 84210 relevant lines covered (92.14%)

11954835.77 hits per line

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

82.61
/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
#include "../tests/test_rng.h" // FIXME
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
   {
20
   public:
21
      Entropy() : Command("entropy --truncate-at=128 source") {}
4✔
22

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

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

33
      void go() override
1✔
34
         {
35
         const std::string req_source = get_arg("source");
1✔
36
         const size_t truncate_sample = get_arg_sz("truncate-at");
1✔
37

38
         auto& entropy_sources = Botan::Entropy_Sources::global_sources();
1✔
39

40
         std::vector<std::string> sources;
1✔
41
         if(req_source == "all")
1✔
42
            sources = entropy_sources.enabled_sources();
1✔
43
         else
44
            sources.push_back(req_source);
×
45

46
         for(const std::string& source : sources)
5✔
47
            {
48
            Botan_Tests::SeedCapturing_RNG rng;
4✔
49
            const size_t entropy_estimate = entropy_sources.poll_just(rng, source);
4✔
50

51
            if(rng.samples() == 0)
4✔
52
               {
53
               output() << "Source " << source << " is unavailable\n";
×
54
               continue;
×
55
               }
56

57
            const auto& sample = rng.seed_material();
4✔
58

59
            output() << "Polling " << source << " gathered " << sample.size()
4✔
60
                     << " bytes in " << rng.samples() << " outputs with estimated entropy "
4✔
61
                     << entropy_estimate << "\n";
4✔
62

63
#if defined(BOTAN_HAS_COMPRESSION)
64
            if(!sample.empty())
4✔
65
               {
66
               auto comp = Botan::Compression_Algorithm::create("zlib");
4✔
67
               if(comp)
4✔
68
                  {
69
                  try
4✔
70
                     {
71
                     Botan::secure_vector<uint8_t> compressed;
4✔
72
                     compressed.assign(sample.begin(), sample.end());
4✔
73
                     comp->start(9);
4✔
74
                     comp->finish(compressed);
4✔
75

76
                     if(compressed.size() < sample.size())
4✔
77
                        {
78
                        output() << "Sample from " << source << " was zlib compressed from " << sample.size()
×
79
                                 << " bytes to " << compressed.size() << " bytes\n";
×
80
                        }
81
                     }
4✔
82
                  catch(std::exception& e)
×
83
                     {
84
                     error_output() << "Error while attempting to compress: " << e.what() << "\n";
×
85
                     }
×
86
                  }
87
               }
4✔
88
#endif
89

90
            if(sample.size() <= truncate_sample)
4✔
91
               {
92
               output() << Botan::hex_encode(sample) << "\n";
2✔
93
               }
94
            else if(truncate_sample > 0)
3✔
95
               {
96
               output() << Botan::hex_encode(&sample[0], truncate_sample) << "...\n";
9✔
97
               }
98
            }
4✔
99
         }
1✔
100
   };
101

102
BOTAN_REGISTER_COMMAND("entropy", Entropy);
2✔
103

104
}
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