Skip to content

Commit

Permalink
feat:add options for password generation (Issue #318) (#321)
Browse files Browse the repository at this point in the history
* feat:add options for password generation (Issue #318)

* feat:add options for password generation (Issue #318) 101a

* feat:add options for password generation (Issue #318) 101b
  • Loading branch information
qmohitsingh authored Nov 23, 2023
1 parent 4486fba commit 111f95b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
9 changes: 8 additions & 1 deletion include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ enum class IPv4Class
C
};

struct PasswordOptions {
bool upperLetters = true;
bool lowerLetters = true;
bool numbers = true;
bool symbols = true;
};

class Internet
{
public:
Expand Down Expand Up @@ -100,7 +107,7 @@ class Internet
* Internet::password(25) // "xv8vDu*wM!Rg0$zd0kH%8p!WY"
* @endcode
*/
static std::string password(int length = 15);
static std::string password(int length = 15, PasswordOptions options = {});

/**
* @brief Returns a random emoji.
Expand Down
25 changes: 18 additions & 7 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "faker-cxx/String.h"
#include "faker-cxx/Word.h"
#include "fmt/format.h"
#include "../string/data/Characters.h"

namespace faker
{
Expand Down Expand Up @@ -93,16 +94,26 @@ std::string Internet::exampleEmail(std::optional<std::string> firstName, std::op
Helper::arrayElement<std::string>(emailExampleHosts));
}

std::string Internet::password(int length)
std::string Internet::password(int length, PasswordOptions options)
{
const std::string passwordCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/";
std::string characters;

std::string password;
if (options.upperLetters) {
characters += faker::upperCharacters;
}
if (options.lowerLetters) {
characters += faker::lowerCharacters;
}
if (options.numbers) {
characters += faker::numericCharacters;
}
if (options.symbols) {
characters += faker::symbolCharacters;
}

for (int i = 0; i < length; i++)
{
password += Helper::arrayElement<char>(passwordCharacters);
std::string password;
for (int i = 0; i < length; ++i) {
password += Helper::arrayElement<char>(characters);
}

return password;
Expand Down
1 change: 1 addition & 0 deletions src/modules/string/data/Characters.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ const std::string upperAlphanumericCharacters = upperCharacters + numericCharact
const std::string mixedAlphanumericCharacters = upperCharacters + lowerCharacters + numericCharacters;
const std::string hexUpperCharacters = "0123456789ABCDEF";
const std::string hexLowerCharacters = "0123456789abcdef";
const std::string symbolCharacters = "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/";
}

0 comments on commit 111f95b

Please sign in to comment.