• 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

94.59
/src/cli/pbkdf.cpp
1
/*
2
* (C) 2018 Ribose Inc
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_PASSWORD_HASHING)
10
   #include <botan/pwdhash.h>
11
   #include <botan/internal/os_utils.h>
12
#endif
13

14
namespace Botan_CLI {
15

16
#if defined(BOTAN_HAS_PASSWORD_HASHING)
17

18
class PBKDF_Tune final : public Command {
19
   public:
20
      PBKDF_Tune() : Command("pbkdf_tune --algo=Scrypt --max-mem=256 --tune-msec=10 --output-len=32 --check *times") {}
8✔
21

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

24
      std::string description() const override { return "Tune a PBKDF algo"; }
1✔
25

26
      void go() override {
3✔
27
         const size_t output_len = get_arg_sz("output-len");
3✔
28
         const size_t max_mem = get_arg_sz("max-mem");
3✔
29
         const auto tune_msec = std::chrono::milliseconds(get_arg_sz("tune-msec"));
3✔
30
         const std::string algo = get_arg("algo");
3✔
31
         const bool check_time = flag_set("check");
3✔
32

33
         auto pwdhash_fam = Botan::PasswordHashFamily::create(algo);
3✔
34

35
         if(!pwdhash_fam)
3✔
36
            throw CLI_Error_Unsupported("Password hashing", algo);
×
37

38
         for(const std::string& time : get_arg_list("times")) {
15✔
39
            std::unique_ptr<Botan::PasswordHash> pwhash;
12✔
40

41
            if(time == "default") {
12✔
42
               pwhash = pwdhash_fam->default_params();
3✔
43
            } else {
44
               size_t msec = 0;
9✔
45
               try {
9✔
46
                  msec = std::stoul(time);
9✔
47
               } catch(std::exception&) { throw CLI_Usage_Error("Unknown time value '" + time + "' for pbkdf_tune"); }
×
48

49
               pwhash = pwdhash_fam->tune(output_len, std::chrono::milliseconds(msec), max_mem, tune_msec);
9✔
50
            }
51

52
            output() << "For " << time << " ms selected " << pwhash->to_string();
24✔
53

54
            if(pwhash->total_memory_usage() > 0) {
12✔
55
               output() << " using " << pwhash->total_memory_usage() / (1024 * 1024) << " MiB";
8✔
56
            }
57

58
            if(check_time) {
12✔
59
               std::vector<uint8_t> outbuf(output_len);
12✔
60
               const uint8_t salt[8] = {0};
12✔
61

62
               const uint64_t start_ns = Botan::OS::get_system_timestamp_ns();
12✔
63
               pwhash->derive_key(outbuf.data(), outbuf.size(), "test", 4, salt, sizeof(salt));
12✔
64
               const uint64_t end_ns = Botan::OS::get_system_timestamp_ns();
12✔
65
               const uint64_t dur_ns = end_ns - start_ns;
12✔
66

67
               output() << " took " << (dur_ns / 1000000.0) << " msec to compute";
12✔
68
            }
12✔
69

70
            output() << "\n";
12✔
71
         }
15✔
72
      }
3✔
73
};
74

75
BOTAN_REGISTER_COMMAND("pbkdf_tune", PBKDF_Tune);
4✔
76

77
#endif
78

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