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