MySQL 8.4.0
Source Code Documentation
random_generator.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2017, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_RANDOM_GENERATOR_INCLUDED
27#define MYSQL_HARNESS_RANDOM_GENERATOR_INCLUDED
28
29#include <random>
30#include <string>
31
32#include "harness_export.h"
33
34namespace mysql_harness {
35
36class HARNESS_EXPORT RandomGeneratorInterface {
37 public:
38 enum AlphabetContent : unsigned {
39 AlphabetDigits = 0x1,
40 AlphabetLowercase = 0x2,
41 AlphabetUppercase = 0x4,
42 AlphabetSpecial = 0x8,
43 AlphabetAll = 0xFF
44 };
45
46 /** @brief Generates a random string out of selected alphabet
47 *
48 * @param length length of string requested
49 * @param alphabet_mask bitmasmask indicating which alphabet symbol groups
50 * should be used for identifier generation (see AlphabetContent enum for
51 * possible values that can be or-ed)
52 * @return string with the generated random chars
53 *
54 * @throws std::invalid_argument when the alphabet_mask is empty or invalid
55 *
56 */
57 virtual std::string generate_identifier(
58 unsigned length, unsigned alphabet_mask = AlphabetAll) = 0;
59
60 /** @brief Generates a random password that adheres to the STRONG password
61 * requirements:
62 * * contains at least 1 digit
63 * * contains at least 1 uppercase letter
64 * * contains at least 1 lowercase letter
65 * * contains at least 1 special character
66 *
67 * @param length length of requested password (should be at least 8)
68 * @return string with the generated password
69 *
70 * @throws std::invalid_argument when the requested length is less than 8
71 *
72 */
73 virtual std::string generate_strong_password(unsigned length) = 0;
74
75 explicit RandomGeneratorInterface() = default;
78 default;
80};
81
82class HARNESS_EXPORT RandomGenerator : public RandomGeneratorInterface {
83 std::mt19937 urng;
84
85 public:
86 RandomGenerator() : urng(std::random_device().operator()()) {}
87 std::string generate_identifier(
88 unsigned length, unsigned alphabet_mask = AlphabetAll) override;
89 std::string generate_strong_password(unsigned length) override;
90};
91
92class HARNESS_EXPORT FakeRandomGenerator : public RandomGeneratorInterface {
93 public:
94 // returns "012345678901234567890123...", truncated to password_length
95 std::string generate_identifier(unsigned length, unsigned) override;
96 // returns "012345678901234567890123...", truncated to password_length
97 std::string generate_strong_password(unsigned length) override;
98};
99
100} // namespace mysql_harness
101
102#endif // MYSQL_HARNESS_RANDOM_GENERATOR_INCLUDED
Definition: random_generator.h:92
Definition: random_generator.h:36
RandomGeneratorInterface & operator=(const RandomGeneratorInterface &)=default
virtual std::string generate_identifier(unsigned length, unsigned alphabet_mask=AlphabetAll)=0
Generates a random string out of selected alphabet.
AlphabetContent
Definition: random_generator.h:38
virtual std::string generate_strong_password(unsigned length)=0
Generates a random password that adheres to the STRONG password requirements:
RandomGeneratorInterface(const RandomGeneratorInterface &)=default
Definition: random_generator.h:82
std::mt19937 urng
Definition: random_generator.h:83
RandomGenerator()
Definition: random_generator.h:86
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: common.h:42
Definition: gcs_xcom_synode.h:64