PRCYCoin  2.0.0.7rc1
P2P Digital Currency
time_estimates.cpp
Go to the documentation of this file.
2 
3 #include <zxcvbn/common.hpp>
4 #include <zxcvbn/util.hpp>
5 
6 #include <sstream>
7 #include <vector>
8 #include <tuple>
9 
10 #include <cmath>
11 
12 namespace zxcvbn {
13 
14 static
15 std::string display_time(time_t seconds);
16 
17 static
18 score_t guesses_to_score(guesses_t guesses);
19 
21  AttackTimes toret;
22 
23 #define SET_CRACK_TIME(a, val) \
24  do { \
25  toret.crack_times_seconds.a = val; \
26  toret.crack_times_display.a = display_time(toret.crack_times_seconds.a); \
27  } \
28  while (false)
29 
30  SET_CRACK_TIME(online_throttling_100_per_hour, guesses / (100.0 / 3600));
31  SET_CRACK_TIME(online_no_throttling_10_per_second, guesses / 10);
32  SET_CRACK_TIME(offline_slow_hashing_1e4_per_second, guesses / 1e4);
33  SET_CRACK_TIME(offline_fast_hashing_1e10_per_second, guesses / 1e10);
34 
35 #undef SET_CRACK_TIME
36 
37  toret.score = guesses_to_score(guesses);
38 
39  return toret;
40 }
41 
42 static
43 score_t guesses_to_score(guesses_t guesses) {
44  auto DELTA = 5;
45  if (guesses < 1e3 + DELTA) {
46  // risky password: "too guessable"
47  return 0;
48  }
49  else if (guesses < 1e6 + DELTA) {
50  // modest protection from throttled online attacks: "very guessable"
51  return 1;
52  }
53  else if (guesses < 1e8 + DELTA) {
54  // modest protection from unthrottled online attacks: "somewhat guessable"
55  return 2;
56  }
57  else if (guesses < 1e10 + DELTA) {
58  // modest protection from offline attacks: "safely unguessable"
59  // assuming a salted, slow hash function like bcrypt, scrypt, PBKDF2, argon, etc
60  return 3;
61  }
62  else {
63  // strong protection from offline attacks under same scenario: "very unguessable"
64  return 4;
65  }
66 }
67 
68 static
69 std::string display_time(time_t seconds) {
70  auto minute = static_cast<time_t>(60);
71  auto hour = minute * 60;
72  auto day = hour * 24;
73  auto month = day * 31;
74  auto year = month * 12;
75  auto century = year * 100;
76 
77  time_t display_num;
78  std::string display_str;
79 
80  std::tie(display_num, display_str) = [&] () -> std::pair<time_t, std::string> {
81  if (seconds < 1) {
82  return {0, "less than a second"};
83  }
84  if (seconds < minute) {
85  auto base = util::round_div(seconds, 1);
86  return {base, "second"};
87  }
88  else if (seconds < hour) {
89  auto base = util::round_div(seconds, minute);
90  return {base, "minute"};
91  }
92  else if (seconds < day) {
93  auto base = util::round_div(seconds, hour);
94  return {base, "hour"};
95  }
96  else if (seconds < month) {
97  auto base = util::round_div(seconds, day);
98  return {base, "day"};
99  }
100  else if (seconds < year) {
101  auto base = util::round_div(seconds, month);
102  return {base, "month"};
103  }
104  else if (seconds < century) {
105  auto base = util::round_div(seconds, year);
106  return {base, "year"};
107  }
108  else {
109  return {0, "centuries"};
110  }
111  }();
112 
113  if (display_num) {
114  std::ostringstream os;
115  os << display_num << " " << display_str;
116  display_str = os.str();
117 
118  if (display_num != 1) {
119  display_str += "s";
120  }
121  }
122 
123 
124  return display_str;
125 }
126 
127 }
zxcvbn::score_t
unsigned score_t
Definition: common.hpp:17
zxcvbn::estimate_attack_times
AttackTimes estimate_attack_times(guesses_t guesses)
Definition: time_estimates.cpp:20
common.hpp
SET_CRACK_TIME
#define SET_CRACK_TIME(a, val)
zxcvbn
Definition: _frequency_lists.cpp:7
zxcvbn::guesses_t
zxcvbn_guesses_t guesses_t
Definition: common.hpp:15
zxcvbn::util::round_div
constexpr auto round_div(T a, T2 b) -> std::enable_if_t< std::is_integral< decltype(a/b)>::value, long >
Definition: util.hpp:14
zxcvbn::AttackTimes
Definition: time_estimates.hpp:12
zxcvbn::AttackTimes::score
score_t score
Definition: time_estimates.hpp:27
time_estimates.hpp
zxcvbn::time_t
double time_t
Definition: time_estimates.hpp:10
util.hpp