|
6 | 6 | #include <chrono> |
7 | 7 | #include <stdexcept> |
8 | 8 | namespace { |
9 | | - static const std::string ALPHABET = |
| 9 | + static const std::string alphabet62 = |
10 | 10 | "0123456789" |
11 | 11 | "abcdefghijklmnopqrstuvwxyz" |
12 | 12 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
13 | | - static const int M = (int)ALPHABET.size(); // 62 |
| 13 | + static const int BASE62 = (int)alphabet62.size(); // 62 |
14 | 14 |
|
15 | 15 | static int random_index() { |
16 | 16 | static std::mt19937 rng( |
17 | 17 | (unsigned)std::chrono::steady_clock::now().time_since_epoch().count() |
18 | 18 | ); |
19 | | - std::uniform_int_distribution<int> dist(0, M - 1); |
| 19 | + std::uniform_int_distribution<int> dist(0, BASE62 - 1); |
20 | 20 | return dist(rng); |
21 | 21 | } |
22 | 22 |
|
23 | 23 | static int idx_of(char c) { |
24 | | - auto pos = ALPHABET.find(c); |
| 24 | + auto pos = alphabet62.find(c); |
25 | 25 | if (pos == std::string::npos) { |
26 | 26 | throw std::invalid_argument(std::string("Invalid char: ") + c); |
27 | 27 | } |
@@ -36,14 +36,14 @@ static std::string simple_encrypt(const std::string& plain) { |
36 | 36 | throw std::invalid_argument("Plaintext cannot be empty"); |
37 | 37 | } |
38 | 38 | int ki = random_index(); |
39 | | - char key = ALPHABET[ki]; |
| 39 | + char key = alphabet62[ki]; |
40 | 40 | std::string cipher; |
41 | 41 | cipher.reserve(N + 1); |
42 | 42 | cipher.push_back(key); |
43 | 43 | for (size_t i = 0; i < N; ++i) { |
44 | 44 | int pi = idx_of(plain[i]); |
45 | | - int ci = (pi + ki + (int)i) % M; |
46 | | - cipher.push_back(ALPHABET[ci]); |
| 45 | + int ci = (pi + ki + (int)i) % BASE62; |
| 46 | + cipher.push_back(alphabet62[ci]); |
47 | 47 | } |
48 | 48 | return cipher; |
49 | 49 | } |
@@ -63,9 +63,36 @@ static std::string simple_decrypt(const std::string& cipher) { |
63 | 63 |
|
64 | 64 | for (size_t i = 0; i < N; ++i) { |
65 | 65 | int ci = idx_of(cipher[i + 1]); |
66 | | - int pi = (ci - ki - (int)i) % M; |
67 | | - if (pi < 0) pi += M; |
68 | | - plain.push_back(ALPHABET[pi]); |
| 66 | + int pi = (ci - ki - (int)i) % BASE62; |
| 67 | + if (pi < 0) pi += BASE62; |
| 68 | + plain.push_back(alphabet62[pi]); |
69 | 69 | } |
70 | 70 | return plain; |
| 71 | +} |
| 72 | + |
| 73 | +static std::string to_base62(std::size_t num) { |
| 74 | + if (num == 0) { |
| 75 | + return std::string(1, alphabet62[0]); |
| 76 | + } |
| 77 | + std::string s; |
| 78 | + while (num > 0) { |
| 79 | + int rem = static_cast<int>(num % BASE62); |
| 80 | + s.push_back(alphabet62[rem]); |
| 81 | + num /= BASE62; |
| 82 | + } |
| 83 | + std::reverse(s.begin(), s.end()); |
| 84 | + return s; |
| 85 | +} |
| 86 | + |
| 87 | +static std::size_t from_base62(const std::string& s) { |
| 88 | + std::size_t val = 0; |
| 89 | + for (char c : s) { |
| 90 | + std::size_t idx = alphabet62.find(c); |
| 91 | + if (idx == std::string::npos) { |
| 92 | + throw std::invalid_argument( |
| 93 | + std::string("Invalid character in base62 string: ") + c); |
| 94 | + } |
| 95 | + val = val * BASE62 + idx; |
| 96 | + } |
| 97 | + return val; |
71 | 98 | } |