25#ifndef ROUTER_KDF_SHA_CRYPT_INCLUDED
26#define ROUTER_KDF_SHA_CRYPT_INCLUDED
46 static std::string salt();
47 static std::string derive(
Type digest,
unsigned long rounds,
48 const std::string &salt,
57 static std::string
base64_encode(
const std::vector<uint8_t> &data);
74 return std::make_pair(
false, std::string{});
77 static std::pair<bool, Type>
type(
const std::string &
name)
noexcept {
86 return std::make_pair(
false,
Type{});
120 static constexpr unsigned long kDefaultRounds = 5000;
124 static constexpr unsigned long kMinRounds = 1000;
128 static constexpr unsigned long kMaxRounds = 999999999;
134 static constexpr size_t kMaxSaltLength = 16;
137 const std::string &checksum)
138 : digest_{digest}, rounds_{rounds}, salt_{salt}, checksum_{checksum} {
140 if (digest != Type::CachingSha2Password && salt_.size() > kMaxSaltLength) {
141 salt_.resize(kMaxSaltLength);
145 if (rounds_ < kMinRounds) rounds_ = kMinRounds;
146 if (rounds_ > kMaxRounds) rounds_ = kMaxRounds;
158 if (
r.first)
return r.second;
160 throw std::invalid_argument(
"failed to map digest to a name");
175 std::string
salt()
const {
return salt_; }
187 unsigned long rounds()
const {
return rounds_; }
216 std::string to_mcf()
const;
224 checksum_ = kdf_type::derive(digest_, rounds_, salt_,
password);
228 return mcf_type::supports_name(mcf_id);
231 static std::error_code
validate(
const std::string &mcf_line,
234 auto me = from_mcf(mcf_line);
235 if (kdf_type::derive(me.digest(), me.rounds(), me.salt(),
password) ==
241 }
catch (
const std::exception &) {
Definition: kdf_sha_crypt.h:254
static constexpr unsigned long kCachingSha2SaltLength
Definition: kdf_sha_crypt.h:258
static ShaCryptMcfAdaptor from_mcf(const std::string &crypt_data)
Definition: kdf_sha_crypt.cc:125
Definition: authentication.h:43
MCF reader/writer for ShaCrypt.
Definition: kdf_sha_crypt.h:110
static std::error_code validate(const std::string &mcf_line, const std::string &password)
Definition: kdf_sha_crypt.h:231
static bool supports_mcf_id(const std::string mcf_id)
Definition: kdf_sha_crypt.h:227
unsigned long rounds_
Definition: kdf_sha_crypt.h:249
std::string checksum_
Definition: kdf_sha_crypt.h:251
std::string mcf_digest_name() const
name of the digest according to MCF.
Definition: kdf_sha_crypt.h:156
void hash(const std::string &password)
hash a password into checksum.
Definition: kdf_sha_crypt.h:223
std::string salt() const
salt.
Definition: kdf_sha_crypt.h:175
ShaCryptMcfAdaptor(Type digest, unsigned long rounds, const std::string &salt, const std::string &checksum)
Definition: kdf_sha_crypt.h:136
unsigned long rounds() const
rounds.
Definition: kdf_sha_crypt.h:187
Type digest() const
Definition: kdf_sha_crypt.h:180
Type digest_
Definition: kdf_sha_crypt.h:248
std::string salt_
Definition: kdf_sha_crypt.h:250
std::string checksum() const
checksum.
Definition: kdf_sha_crypt.h:168
Definition: kdf_sha_crypt.h:60
static bool supports_name(const std::string &name) noexcept
Definition: kdf_sha_crypt.h:89
static constexpr char kTypeSha512[]
Definition: kdf_sha_crypt.h:103
static constexpr char kTypeSha256[]
Definition: kdf_sha_crypt.h:102
static std::pair< bool, Type > type(const std::string &name) noexcept
Definition: kdf_sha_crypt.h:77
static std::pair< bool, std::string > name(Type type) noexcept
Definition: kdf_sha_crypt.h:64
static constexpr char kTypeCachingSha2Password[]
Definition: kdf_sha_crypt.h:104
sha256_crypt and sha512_crypt are SHA based crypt() key derivation functions.
Definition: kdf_sha_crypt.h:43
Type
Definition: kdf_sha_crypt.h:45
#define HTTP_AUTH_BACKEND_LIB_EXPORT
Definition: http_auth_backend_lib_export.h:15
static int base64_encode(const void *src, size_t src_len, char *dst)
Definition: base64.h:255
static char * password
Definition: mysql_secure_installation.cc:56
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
case opt name
Definition: sslopt-case.h:33
Constants and functionality that facilitate working with digests.