25#ifndef MYSQLROUTER_KDF_PBKDF2_INCLUDED
26#define MYSQLROUTER_KDF_PBKDF2_INCLUDED
61 enum class Type { Sha_256, Sha_512 };
62 static std::vector<uint8_t> salt();
63 static std::vector<uint8_t> derive(
Type type,
unsigned long rounds,
64 const std::vector<uint8_t> &salt,
65 const std::string &
key);
89 return std::make_pair(
false, std::string{});
92 static std::pair<bool, Type>
type(
const std::string &
name)
noexcept {
99 return std::make_pair(
false,
Type{});
125 static constexpr unsigned long kDefaultRounds = 1000;
129 static constexpr unsigned long kMinRounds = 1;
133 static constexpr unsigned long kMaxRounds = 999999999;
139 static constexpr size_t kMaxSaltLength = 16;
142 const std::vector<uint8_t> &salt,
143 const std::vector<uint8_t> &checksum)
144 : type_{
type}, rounds_{rounds}, salt_{salt}, checksum_{checksum} {
146 if (salt_.size() > kMaxSaltLength) {
147 salt_.resize(kMaxSaltLength);
151 if (rounds_ < kMinRounds) rounds_ = kMinRounds;
152 if (rounds_ > kMaxRounds) rounds_ = kMaxRounds;
163 if (
r.first)
return r.second;
165 throw std::invalid_argument(
"failed to map digest to a name");
173 std::vector<uint8_t>
checksum()
const {
return checksum_; }
180 std::vector<uint8_t>
salt()
const {
return salt_; }
192 unsigned long rounds()
const {
return rounds_; }
219 std::string to_mcf()
const;
237 static std::string
base64_encode(
const std::vector<uint8_t> &encoded);
245 checksum_ = kdf_type::derive(type_, rounds(), salt(),
key);
249 return mcf_type::supports_name(mcf_id);
252 static std::error_code
validate(
const std::string &mcf_line,
255 auto mcf = from_mcf(mcf_line);
256 if (kdf_type::derive(mcf.digest(), mcf.rounds(), mcf.salt(),
password) ==
262 }
catch (
const std::exception &) {
MCF reader/writer for PBKDF2.
Definition: kdf_pbkdf2.h:116
std::vector< uint8_t > checksum() const
checksum.
Definition: kdf_pbkdf2.h:173
void derive(const std::string &key)
derive a checksum from a key.
Definition: kdf_pbkdf2.h:244
unsigned long rounds_
Definition: kdf_pbkdf2.h:270
static std::error_code validate(const std::string &mcf_line, const std::string &password)
Definition: kdf_pbkdf2.h:252
std::vector< uint8_t > salt() const
salt.
Definition: kdf_pbkdf2.h:180
std::vector< uint8_t > salt_
Definition: kdf_pbkdf2.h:271
unsigned long rounds() const
rounds.
Definition: kdf_pbkdf2.h:192
std::vector< uint8_t > checksum_
Definition: kdf_pbkdf2.h:272
std::string mcf_id() const
name of the digest according to MCF.
Definition: kdf_pbkdf2.h:161
static bool supports_mcf_id(const std::string mcf_id)
Definition: kdf_pbkdf2.h:248
Type digest() const
Definition: kdf_pbkdf2.h:185
Type type_
Definition: kdf_pbkdf2.h:269
Pbkdf2McfAdaptor(Type type, unsigned long rounds, const std::vector< uint8_t > &salt, const std::vector< uint8_t > &checksum)
Definition: kdf_pbkdf2.h:141
map the MCF name to internal types.
Definition: kdf_pbkdf2.h:75
static bool supports_name(const std::string &name) noexcept
Definition: kdf_pbkdf2.h:102
static constexpr char kTypeSha256[]
Definition: kdf_pbkdf2.h:78
static std::pair< bool, std::string > name(Type type) noexcept
Definition: kdf_pbkdf2.h:81
static std::pair< bool, Type > type(const std::string &name) noexcept
Definition: kdf_pbkdf2.h:92
static constexpr char kTypeSha512[]
Definition: kdf_pbkdf2.h:79
Key Derivation Function for PBKDF2.
Definition: kdf_pbkdf2.h:59
Type
Definition: kdf_pbkdf2.h:61
#define HTTP_AUTH_BACKEND_LIB_EXPORT
Definition: http_auth_backend_lib_export.h:15
static int64 base64_decode(const char *src_base, size_t len, void *dst, const char **end_ptr, int flags)
Decode a base64 string The base64-encoded data in the range ['src','*end_ptr') will be decoded and st...
Definition: base64.h:304
static int base64_encode(const void *src, size_t src_len, char *dst)
Definition: base64.h:243
static char * password
Definition: mysql_secure_installation.cc:58
constexpr value_type binary
Definition: classic_protocol_constants.h:275
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
Type
Definition: resource_group_basic_types.h:33
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required string type
Definition: replication_group_member_actions.proto:34
case opt name
Definition: sslopt-case.h:29