From 111f95b29101a42c0db6d012b61331385f7102c3 Mon Sep 17 00:00:00 2001 From: Mohit Singh <30595867+qmohitsingh@users.noreply.github.com> Date: Thu, 23 Nov 2023 15:56:08 -0500 Subject: [PATCH] feat:add options for password generation (Issue #318) (#321) * 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 --- include/faker-cxx/Internet.h | 9 ++++++++- src/modules/internet/Internet.cpp | 25 ++++++++++++++++++------- src/modules/string/data/Characters.h | 1 + 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/include/faker-cxx/Internet.h b/include/faker-cxx/Internet.h index f9209df8a..61fd0d547 100644 --- a/include/faker-cxx/Internet.h +++ b/include/faker-cxx/Internet.h @@ -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: @@ -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. diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index ec1033be7..ab79b7d8e 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -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 { @@ -93,16 +94,26 @@ std::string Internet::exampleEmail(std::optional firstName, std::op Helper::arrayElement(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(passwordCharacters); + std::string password; + for (int i = 0; i < length; ++i) { + password += Helper::arrayElement(characters); } return password; diff --git a/src/modules/string/data/Characters.h b/src/modules/string/data/Characters.h index f448daf26..2cf093b0a 100644 --- a/src/modules/string/data/Characters.h +++ b/src/modules/string/data/Characters.h @@ -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 = "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/"; }