CppSecurity 1.1.2.0
C++ Security Library
Loading...
Searching...
No Matches
password_hashing.cpp
Go to the documentation of this file.
1
10
11#include "errors/exceptions.h"
12#include "memory/memory.h"
13#include "string/encoding.h"
14
15#include <cassert>
16
17namespace CppSecurity {
18
19PasswordHashing::PasswordHashing(size_t hash_length, size_t salt_length)
20 : _hash_length(hash_length), _salt_length(salt_length)
21{
22 assert((hash_length >= 8) && "Hash length should be at least 8 bytes!");
23 if (hash_length < 8)
24 throwex CppCommon::SecurityException("Invalid hash length!");
25
26 assert((salt_length >= 8) && "Salt length should be at least 8 bytes!");
27 if (salt_length < 8)
28 throwex CppCommon::SecurityException("Invalid salt length!");
29}
30
32{
33 std::string salt(salt_length(), 0);
34 CppCommon::Memory::CryptoFill(salt.data(), salt.size());
35 return salt;
36}
37
38std::pair<std::string, std::string> PasswordHashing::GenerateHashAndSalt(std::string_view password) const
39{
40 std::string salt = GenerateSalt();
41 std::string hash = GenerateHash(password, salt);
42 return std::make_pair(hash, salt);
43}
44
45std::string PasswordHashing::GenerateDigest(std::string_view password) const
46{
47 auto digest = GenerateHashAndSalt(password);
48 return digest.first + digest.second;
49}
50
51std::string PasswordHashing::GenerateEncodedDigest(std::string_view password) const
52{
53 // Encode the digest into the Base64 encoding
54 return CppCommon::Encoding::Base64Encode(GenerateDigest(password));
55}
56
57bool PasswordHashing::ValidateDigest(std::string_view password, std::string_view digest) const
58{
59 // Check the digest size (must be hash + salt)
60 if (digest.size() != (hash_length() + salt_length()))
61 return false;
62
63 // Extract hash and salt from the digest
64 std::string_view hash(digest.data(), hash_length());
65 std::string_view salt(digest.data() + hash_length(), salt_length());
66
67 // Perform the password validation
68 return Validate(password, hash, salt);
69}
70
71bool PasswordHashing::ValidateEncodedDigest(std::string_view password, std::string_view digest) const
72{
73 // Decode the digest from the Base64 encoding
74 return ValidateDigest(password, CppCommon::Encoding::Base64Decode(digest));
75}
76
77} // namespace CppSecurity
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.