Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add options for password generation (Issue #318) #321

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/";
}