MySQL 8.0.29
Source Code Documentation
random_generator.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2017, 2021, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_RANDOM_GENERATOR_INCLUDED
26#define MYSQL_HARNESS_RANDOM_GENERATOR_INCLUDED
27
28#include <random>
29#include <string>
30
31#include "harness_export.h"
32
33namespace mysql_harness {
34
35class HARNESS_EXPORT RandomGeneratorInterface {
36 public:
37 enum AlphabetContent : unsigned {
38 AlphabetDigits = 0x1,
39 AlphabetLowercase = 0x2,
40 AlphabetUppercase = 0x4,
41 AlphabetSpecial = 0x8,
42 AlphabetAll = 0xFF
43 };
44
45 /** @brief Generates a random string out of selected alphabet
46 *
47 * @param length length of string requested
48 * @param alphabet_mask bitmasmask indicating which alphabet symbol groups
49 * should be used for indentifier generation (see AlphabetContent enum for
50 * possible values that can be or-ed)
51 * @return string with the generated random chars
52 *
53 * @throws std::invalid_argument when the alphabet_mask is empty or invalid
54 *
55 */
56 virtual std::string generate_identifier(
57 unsigned length, unsigned alphabet_mask = AlphabetAll) = 0;
58
59 /** @brief Generates a random password that adheres to the STRONG password
60 * requirements:
61 * * contains at least 1 digit
62 * * contains at least 1 uppercase letter
63 * * contains at least 1 lowercase letter
64 * * contains at least 1 special character
65 *
66 * @param length length of requested password (should be at least 8)
67 * @return string with the generated password
68 *
69 * @throws std::invalid_argument when the requested length is less than 8
70 *
71 */
72 virtual std::string generate_strong_password(unsigned length) = 0;
73
74 explicit RandomGeneratorInterface() = default;
77 default;
79};
80
81class HARNESS_EXPORT RandomGenerator : public RandomGeneratorInterface {
82 std::mt19937 urng;
83
84 public:
85 RandomGenerator() : urng(std::random_device().operator()()) {}
86 std::string generate_identifier(
87 unsigned length, unsigned alphabet_mask = AlphabetAll) override;
88 std::string generate_strong_password(unsigned length) override;
89};
90
91class HARNESS_EXPORT FakeRandomGenerator : public RandomGeneratorInterface {
92 public:
93 // returns "012345678901234567890123...", truncated to password_length
94 std::string generate_identifier(unsigned length, unsigned) override;
95 // returns "012345678901234567890123...", truncated to password_length
96 std::string generate_strong_password(unsigned length) override;
97};
98
99} // namespace mysql_harness
100
101#endif // MYSQL_HARNESS_RANDOM_GENERATOR_INCLUDED
Definition: random_generator.h:91
Definition: random_generator.h:35
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:37
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:81
std::mt19937 urng
Definition: random_generator.h:82
RandomGenerator()
Definition: random_generator.h:85
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:75
Definition: common.h:41
Definition: varlen_sort.h:183