Skip to content

Commit

Permalink
feat: integrating database and password
Browse files Browse the repository at this point in the history
  • Loading branch information
stellaljung committed Oct 23, 2023
1 parent 7288ad3 commit c1fae6c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 22 deletions.
61 changes: 61 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__verbose_abort": "cpp",
"array": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"regex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp"
}
}
10 changes: 9 additions & 1 deletion backend/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

#include "crow.h"
#include "database.hpp"
#include "password.hpp"

int main()
{
{
database::Database db = database::Database("passwords.db");
db.execute("CREATE TABLE passwords (password TEXT);");

// generate and insert the passwords into the database
std::unordered_set<std::string> passwordSet = password::generatePasswords(100, 20);
for (const auto &password : passwordSet)
{
db.execute("INSERT INTO passwords (password) VALUES ('" + password + "');");
}
db.execute("INSERT INTO passwords (password) VALUES ('chocolate1');");
db.printTable("passwords");

Expand Down
45 changes: 25 additions & 20 deletions backend/src/password.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

namespace password
{
std::unordered_set<std::string> generatePasswords(const int &numPasswords, const int &maxChars)
std::unordered_set<std::string> generatePasswords(int num_passwords, int max_chars)
{
const int min_chars = 10;

if (maxChars < min_chars) {
if (max_chars < min_chars)
{
std::cout << "Password must have a minimum length of 10 characters\n";
exit(0);
}
Expand All @@ -16,55 +17,59 @@ namespace password
const std::string lowercase = "abcdefghijklmnopqrstuvwxyz";
const std::string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string digits = "0123456789";
const std::string symbols = "!#$%&'()\\*+,-./:;<=>?@[]^_`{|}~\"";
const std::string symbols = "!#$%&()*+,-./:;<=>?@[]^_`{|}~";

while (passwords.size() < numPasswords)
while (passwords.size() < num_passwords)
{
// randomize length between minChars and maxChars
const size_t length = min_chars + rand() % ((maxChars + 1) - min_chars);
std::string password = "";
bool hasLowercase = false;
bool hasUppercase = false;
bool hasDigit = false;
bool hasSymbol = false;
const size_t length = min_chars + rand() % ((max_chars + 1) - min_chars);
std::string password = "";
bool has_lowercase = false;
bool has_uppercase = false;
bool has_digit = false;
bool has_symbol = false;

while (password.length() < length)
{
int charType = rand() % 4;
int char_type = rand() % 4;
int index;
switch (charType)
switch (char_type)
{
case 0: // lowercase letter
if (hasLowercase && !(hasUppercase && hasDigit && hasSymbol)){
if (has_lowercase && !(has_uppercase && has_digit && has_symbol))
{
break;
}
index = rand() % lowercase.length();
password = password + lowercase[index];
hasLowercase = true;
has_lowercase = true;
break;
case 1: // uppercase letter
if (hasUppercase && !(hasLowercase && hasDigit && hasSymbol)) {
if (has_uppercase && !(has_lowercase && has_digit && has_symbol))
{
break;
}
index = rand() % uppercase.length();
password = password + uppercase[index];
hasUppercase = true;
has_uppercase = true;
break;
case 2: // digit
if (hasDigit && !(hasLowercase && hasUppercase && hasSymbol)) {
if (has_digit && !(has_lowercase && has_uppercase && has_symbol))
{
break;
}
index = rand() % digits.length();
password = password + digits[index];
hasDigit = true;
has_digit = true;
break;
case 3: // symbol
if (hasSymbol && !(hasLowercase && hasUppercase && hasDigit)) {
if (has_symbol && !(has_lowercase && has_uppercase && has_digit))
{
break;
}
index = rand() % symbols.length();
password = password + symbols[index];
hasSymbol = true;
has_symbol = true;
break;
};
};
Expand Down
2 changes: 1 addition & 1 deletion backend/src/password.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ namespace password
* @param maxChars The maximum number of characters for a password.
* @return std::unordered_set<std::string>
*/
std::unordered_set<std::string> generatePasswords(const int &numPasswords, const int &maxChars);
std::unordered_set<std::string> generatePasswords(int num_passwords, int max_chars);
}
#endif // PASSWORDS_H

0 comments on commit c1fae6c

Please sign in to comment.