11 #include "errors/exceptions.h"
13 #include <libbcrypt.h>
17 std::string BcryptPasswordHashing::_name =
"bcrypt";
21 _workfactor(workfactor)
28 if (bcrypt_gensalt((
int)
workfactor(), salt.data()) != 0)
29 throwex CppCommon::SecurityException(
"Cannot generate 'bcrypt' salt!");
37 if (bcrypt_hashpw(password.data(), salt.data(), hash.data()) != 0)
38 throwex CppCommon::SecurityException(
"Cannot generate 'bcrypt' hash!");
51 std::string digest(hash.size(), 0);
52 if (bcrypt_hashpw(password.data(), salt.data(), digest.data()) != 0)
53 throwex CppCommon::SecurityException(
"Cannot calculate 'bcrypt' hash!");
56 return (digest == hash);
66 return (bcrypt_checkpw(password.data(), digest.data()) == 0);
bool ValidateDigest(std::string_view password, std::string_view digest) const override
Validate the user password over the given secure digest string.
std::string GenerateDigest(std::string_view password) const override
Generate the secure digest string for the given user password.
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.
std::string GenerateSalt() const override
Generate the unique password 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.
size_t workfactor() const noexcept
Get the work factor.
BcryptPasswordHashing(size_t workfactor=4)
Initialize 'bcrypt' password hashing with required parameters.
Password hashing interface.
size_t salt_length() const noexcept
Get the unique password salt length.
size_t hash_length() const noexcept
Get the strong password hash length.
'bcrypt' password hashing algorithm definition