CppSecurity 1.1.2.0
C++ Security Library
Loading...
Searching...
No Matches
password_hashing_argon2.cpp
Go to the documentation of this file.
1
10
11#include "errors/exceptions.h"
12
13#include <argon2.h>
14
15namespace CppSecurity {
16
17std::string Argon2dPasswordHashing::_name = "Argon2d";
18std::string Argon2iPasswordHashing::_name = "Argon2i";
19std::string Argon2idPasswordHashing::_name = "Argon2id";
20
21Argon2dPasswordHashing::Argon2dPasswordHashing(size_t hash_length, size_t salt_length, uint32_t t, uint32_t m, uint32_t p)
22 : PasswordHashing(hash_length, salt_length),
23 _t(t), _m(m), _p(p)
24{
25}
26
27std::string Argon2dPasswordHashing::GenerateHash(std::string_view password, std::string_view salt) const
28{
29 // Generate the strong password hash
30 std::string hash(hash_length(), 0);
31 if (argon2d_hash_raw(t(), m(), p(), password.data(), password.size(), salt.data(), salt.size(), hash.data(), hash.size()) != ARGON2_OK)
32 throwex CppCommon::SecurityException("Cannot generate 'Argon2d' hash!");
33 return hash;
34}
35
36bool Argon2dPasswordHashing::Validate(std::string_view password, std::string_view hash, std::string_view salt) const
37{
38 // Calculate the digest for the given password and salt
39 std::string digest(hash.size(), 0);
40 if (argon2d_hash_raw(t(), m(), p(), password.data(), password.size(), salt.data(), salt.size(), digest.data(), digest.size()) != ARGON2_OK)
41 throwex CppCommon::SecurityException("Cannot calculate 'Argon2d' hash!");
42
43 // Compare the digest with the given hash
44 return (digest == hash);
45}
46
47std::string Argon2iPasswordHashing::GenerateHash(std::string_view password, std::string_view salt) const
48{
49 // Generate the strong password hash
50 std::string hash(hash_length(), 0);
51 if (argon2i_hash_raw(t(), m(), p(), password.data(), password.size(), salt.data(), salt.size(), hash.data(), hash.size()) != ARGON2_OK)
52 throwex CppCommon::SecurityException("Cannot generate 'Argon2i' hash!");
53 return hash;
54}
55
56bool Argon2iPasswordHashing::Validate(std::string_view password, std::string_view hash, std::string_view salt) const
57{
58 // Calculate the digest for the given password and salt
59 std::string digest(hash.size(), 0);
60 if (argon2i_hash_raw(t(), m(), p(), password.data(), password.size(), salt.data(), salt.size(), digest.data(), digest.size()) != ARGON2_OK)
61 throwex CppCommon::SecurityException("Cannot calculate 'Argon2i' hash!");
62
63 // Compare the digest with the given hash
64 return (digest == hash);
65}
66
67std::string Argon2idPasswordHashing::GenerateHash(std::string_view password, std::string_view salt) const
68{
69 // Generate the strong password hash
70 std::string hash(hash_length(), 0);
71 if (argon2id_hash_raw(t(), m(), p(), password.data(), password.size(), salt.data(), salt.size(), hash.data(), hash.size()) != ARGON2_OK)
72 throwex CppCommon::SecurityException("Cannot generate 'Argon2id' hash!");
73 return hash;
74}
75
76bool Argon2idPasswordHashing::Validate(std::string_view password, std::string_view hash, std::string_view salt) const
77{
78 // Calculate the digest for the given password and salt
79 std::string digest(hash.size(), 0);
80 if (argon2id_hash_raw(t(), m(), p(), password.data(), password.size(), salt.data(), salt.size(), digest.data(), digest.size()) != ARGON2_OK)
81 throwex CppCommon::SecurityException("Cannot calculate 'Argon2id' hash!");
82
83 // Compare the digest with the given hash
84 return (digest == hash);
85}
86
87} // namespace CppSecurity
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.
Argon2dPasswordHashing(size_t hash_length=32, size_t salt_length=32, uint32_t t=3, uint32_t m=512, uint32_t p=1)
Initialize 'Argon2' password hashing with required parameters.
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.
uint32_t p() const noexcept
Get the degree of parallelism.
uint32_t t() const noexcept
Get the number of iterations.
uint32_t m() const noexcept
Get the memory usage in kibibytes.
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.
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.
'Argon2' password hashing algorithm definition