11 #include "errors/exceptions.h"
12 #include "memory/memory.h"
13 #include "utility/countof.h"
20 : _length(length), _flags(flags)
22 assert((
length >= 6) &&
"Password should be at least 6 bytes!");
24 throwex CppCommon::SecurityException(
"Invalid password length!");
29 char lower[] =
"abcdefghijklmnopqrstuvwxyz";
30 char upper[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
31 char digits[] =
"012345678901234567890123456789";
32 char symbols[] =
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
46 CppCommon::Memory::CryptoFill(result.data(), result.size());
49 result[offset] =
lower[result[offset] % (CppCommon::countof(
lower) - 1)], ++offset;
51 result[offset] =
upper[result[offset] % (CppCommon::countof(
upper) - 1)], ++offset;
53 result[offset] =
digits[result[offset] % (CppCommon::countof(
digits) - 1)], ++offset;
55 result[offset] =
symbols[result[offset] % (CppCommon::countof(
symbols) - 1)], ++offset;
57 for (
size_t i = offset; i <
length(); ++i)
58 result[i] = cache[result[i] % cache.size()];
66 if (password.size() <
length())
74 for (
size_t i = 0; i < password.size(); ++i)
76 if ((password[i] >=
'a') && (password[i] <=
'z'))
78 else if ((password[i] >=
'A') && (password[i] <=
'Z'))
80 else if ((password[i] >=
'0') && (password[i] <=
'9'))
82 else if ((password[i] ==
'!') || (password[i] ==
'"') || (password[i] ==
'#') || (password[i] ==
'$') || (password[i] ==
'%') ||
83 (password[i] ==
'&') || (password[i] ==
'\'') || (password[i] ==
'(') || (password[i] ==
')') || (password[i] ==
'*') ||
84 (password[i] ==
'+') ||(password[i] ==
',') || (password[i] ==
'-') || (password[i] ==
'.') || (password[i] ==
'/') ||
85 (password[i] ==
':') || (password[i] ==
';') || (password[i] ==
'<') || (password[i] ==
'=') || (password[i] ==
'>') ||
86 (password[i] ==
'?') || (password[i] ==
'@') || (password[i] ==
'[') || (password[i] ==
'\\') || (password[i] ==
']') ||
87 (password[i] ==
'^') || (password[i] ==
'_') || (password[i] ==
'`') || (password[i] ==
'{') || (password[i] ==
'|') ||
88 (password[i] ==
'}') || (password[i] ==
'~'))
virtual std::password Generate() const
Generate the strong password with the current password requirements.
size_t length() const noexcept
Get the password length.
PasswordGenerator(size_t length=12, PasswordFlags flags=PasswordFlags::lower|PasswordFlags::upper|PasswordFlags::digits)
Initialize password generator with required parameters.
virtual bool Validate(std::string_view password) const
Validate the given password with the current password requirements.
PasswordFlags flags() const noexcept
Get the password flags.
PasswordFlags
Password flags.
@ upper
Password must contain alphabet characters in upper case (A-Z)
@ symbols
Password must contain punctuation symbols (!"#$%&'()*+,-./:;<=>?@[]^_`{|}~)
@ lower
Password must contain alphabet characters in lower case (a-z)
@ digits
Password must contain digits (0-9)
Password generator definition.