11 #include <openssl/evp.h>
15 std::string PBKDF2PasswordHashing::_name =
"PBKDF2";
19 _algorithm(algorithm),
20 _iterations(iterations)
27 const EVP_MD* md =
nullptr;
41 if (PKCS5_PBKDF2_HMAC((
const char*)password.data(), (
int)password.size(), (
const uint8_t*)salt.data(), (
int)salt.size(), (
int)
iterations(), md, (
int)hash.size(), (uint8_t*)hash.data()) == 0)
42 throwex CppCommon::SecurityException(
"Cannot generate 'PBKDF2' hash!");
49 const EVP_MD* md =
nullptr;
50 std::string digest(hash.size(), 0);
63 if (PKCS5_PBKDF2_HMAC((
const char*)password.data(), (
int)password.size(), (
const uint8_t*)salt.data(), (
int)salt.size(), (
int)
iterations(), md, (
int)digest.size(), (uint8_t*)digest.data()) == 0)
64 throwex CppCommon::SecurityException(
"Cannot calculate 'PBKDF2' hash!");
67 return (digest == hash);
PBKDF2PasswordHashing(size_t hash_length=32, size_t salt_length=32, PBKDF2 algorithm=PBKDF2::HMAC_SHA512, size_t iterations=1000)
Initialize 'PBKDF2' password hashing with required parameters.
size_t iterations() const noexcept
Get the count of 'PBKDF2' iterations.
PBKDF2 algorithm() const noexcept
Get the 'PBKDF2' algorithm.
std::string GenerateHash(std::string_view password, std::string_view salt) const override
Generate the strong password hash for the given user password and unique salt.
bool Validate(std::string_view password, std::string_view hash, std::string_view salt) const override
Validate the user password over the given strong password hash and unique salt.
Password hashing interface.
size_t hash_length() const noexcept
Get the strong password hash length.
@ HMAC_SHA256
HMAC-SHA256.
@ HMAC_SHA512
HMAC-SHA512.
'PBKDF2' password hashing algorithm definition