safe
[ghsmtp.git] / Pill.cpp
blob2f18ad946761bf78246be02743748d9d6baa60e2
1 #include "Pill.hpp"
3 Pill::Pill()
5 using s_t = decltype(s_);
7 std::uniform_int_distribution<s_t> uni_dist;
8 s_ = uni_dist(rng_);
10 auto resp{b32_ndigits_};
11 b32_str_[resp] = '\0';
13 // <http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt>
15 constexpr char b32_charset[]{"ybndrfg8ejkmcpqxot1uwisza345h769"};
17 auto const os{reinterpret_cast<const unsigned char*>(&s_)};
18 auto osp{os + sizeof(s_)};
19 auto x{0ul};
21 switch ((osp - os) % 5) { // Duff's device
22 case 0:
23 do {
24 x = *--osp;
25 b32_str_[--resp] = b32_charset[x % 32];
26 x /= 32;
27 [[fallthrough]];
29 case 4:
30 x |= (static_cast<unsigned long>(*--osp)) << 3;
31 b32_str_[--resp] = b32_charset[x % 32];
32 x /= 32;
33 b32_str_[--resp] = b32_charset[x % 32];
34 x /= 32;
35 [[fallthrough]];
37 case 3:
38 x |= (static_cast<unsigned long>(*--osp)) << 1;
39 b32_str_[--resp] = b32_charset[x % 32];
40 x /= 32;
41 [[fallthrough]];
43 case 2:
44 x |= (static_cast<unsigned long>(*--osp)) << 4;
45 b32_str_[--resp] = b32_charset[x % 32];
46 x /= 32;
47 b32_str_[--resp] = b32_charset[x % 32];
48 x /= 32;
49 [[fallthrough]];
51 case 1:
52 x |= (static_cast<unsigned long>(*--osp)) << 2;
53 b32_str_[--resp] = b32_charset[x % 32];
54 x /= 32;
55 b32_str_[--resp] = b32_charset[x];
56 } while (osp > os);