1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include <string_view>
#include "auth/Crypto.h"
#include "common/armor.h"
#include "common/ceph_context.h"
#include "common/dout.h"
#include "random_string.h"
int gen_rand_base64(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */
{
char buf[size];
char tmp_dest[size + 4]; /* so that there's space for the extra '=' characters, and some */
int ret;
cct->random()->get_bytes(buf, sizeof(buf));
ret = ceph_armor(tmp_dest, &tmp_dest[sizeof(tmp_dest)],
(const char *)buf, ((const char *)buf) + ((size - 1) * 3 + 4 - 1) / 4);
if (ret < 0) {
lderr(cct) << "ceph_armor failed" << dendl;
return ret;
}
tmp_dest[ret] = '\0';
memcpy(dest, tmp_dest, size);
dest[size-1] = '\0';
return 0;
}
// choose 'size' random characters from the given string table
static void choose_from(CryptoRandom* random, std::string_view table,
char *dest, size_t size)
{
random->get_bytes(dest, size);
for (size_t i = 0; i < size; i++) {
auto pos = static_cast<unsigned>(dest[i]);
dest[i] = table[pos % table.size()];
}
}
void gen_rand_alphanumeric(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */
{
// this is basically a modified base64 charset, url friendly
static constexpr char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
choose_from(cct->random(), table, dest, size-1);
dest[size-1] = 0;
}
std::string gen_rand_alphanumeric(CephContext *cct, size_t size)
{
std::string str;
str.resize(size + 1);
gen_rand_alphanumeric(cct, str.data(), str.size());
str.pop_back(); // pop the extra \0
return str;
}
void gen_rand_alphanumeric_lower(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */
{
static constexpr char table[] = "0123456789abcdefghijklmnopqrstuvwxyz";
choose_from(cct->random(), table, dest, size-1);
dest[size-1] = 0;
}
std::string gen_rand_alphanumeric_lower(CephContext *cct, size_t size)
{
std::string str;
str.resize(size + 1);
gen_rand_alphanumeric_lower(cct, str.data(), str.size());
str.pop_back(); // pop the extra \0
return str;
}
void gen_rand_alphanumeric_upper(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */
{
static constexpr char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
choose_from(cct->random(), table, dest, size-1);
dest[size-1] = 0;
}
std::string gen_rand_alphanumeric_upper(CephContext *cct, size_t size)
{
std::string str;
str.resize(size + 1);
gen_rand_alphanumeric_upper(cct, str.data(), str.size());
str.pop_back(); // pop the extra \0
return str;
}
void gen_rand_alphanumeric_no_underscore(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */
{
static constexpr char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
choose_from(cct->random(), table, dest, size-1);
dest[size-1] = 0;
}
std::string gen_rand_alphanumeric_no_underscore(CephContext *cct, size_t size)
{
std::string str;
str.resize(size + 1);
gen_rand_alphanumeric_no_underscore(cct, str.data(), str.size());
str.pop_back(); // pop the extra \0
return str;
}
void gen_rand_alphanumeric_plain(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */
{
static constexpr char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
choose_from(cct->random(), table, dest, size-1);
dest[size-1] = 0;
}
std::string gen_rand_alphanumeric_plain(CephContext *cct, size_t size)
{
std::string str;
str.resize(size + 1);
gen_rand_alphanumeric_plain(cct, str.data(), str.size());
str.pop_back(); // pop the extra \0
return str;
}
void gen_rand_numeric(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */
{
static constexpr char table[] = "0123456789";
choose_from(cct->random(), table, dest, size-1);
dest[size-1] = 0;
}
std::string gen_rand_numeric(CephContext *cct, size_t size)
{
std::string str;
str.resize(size + 1);
gen_rand_numeric(cct, str.data(), str.size());
str.pop_back(); // pop the extra \0
return str;
}
|