11 #include "errors/exceptions.h"
12 #include "memory/memory.h"
13 #include "string/encoding.h"
20 : _hash_length(hash_length), _salt_length(salt_length)
22 assert((
hash_length >= 8) &&
"Hash length should be at least 8 bytes!");
24 throwex CppCommon::SecurityException(
"Invalid hash length!");
26 assert((
salt_length >= 8) &&
"Salt length should be at least 8 bytes!");
28 throwex CppCommon::SecurityException(
"Invalid salt length!");
34 CppCommon::Memory::CryptoFill(salt.data(), salt.size());
42 return std::make_pair(hash, salt);
48 return digest.first + digest.second;
54 return CppCommon::Encoding::Base64Encode(
GenerateDigest(password));
64 std::string_view hash(digest.data(),
hash_length());
68 return Validate(password, hash, salt);
74 return ValidateDigest(password, CppCommon::Encoding::Base64Decode(digest));
size_t salt_length() const noexcept
Get the unique password salt length.
virtual std::string GenerateEncodedDigest(std::string_view password) const
Generate the secure Base64 digest string for the given user password.
virtual bool Validate(std::string_view password, std::string_view hash, std::string_view salt) const =0
Validate the user password over the given strong password hash and unique salt.
virtual std::string GenerateSalt() const
Generate the unique password salt.
virtual bool ValidateEncodedDigest(std::string_view password, std::string_view digest) const
Validate the user password over the given secure Base64 digest string.
virtual std::pair< std::string, std::string > GenerateHashAndSalt(std::string_view password) const
Generate the strong password hash and unique salt for the given user password.
PasswordHashing(size_t hash_length=32, size_t salt_length=32)
Initialize password hashing with required parameters.
size_t hash_length() const noexcept
Get the strong password hash length.
virtual std::string GenerateDigest(std::string_view password) const
Generate the secure digest string for the given user password.
virtual bool ValidateDigest(std::string_view password, std::string_view digest) const
Validate the user password over the given secure digest string.
virtual std::string GenerateHash(std::string_view password, std::string_view salt) const =0
Generate the strong password hash for the given user password and unique salt.
Password hashing interface definition.