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

Generate breached passwords script #14

Merged
merged 46 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
0154168
feat: generate breached passwords script
ni-jessica Oct 15, 2023
1f4ef9a
refactor: use hashset
ni-jessica Oct 16, 2023
3c94ef9
Merge branch 'main' into 7-create-breached-password-generator-script
csirianni Oct 20, 2023
8b8c434
refactor: use Python file naming convention
csirianni Oct 20, 2023
08e1b0b
refactor: rewrite generate_passwords
csirianni Oct 20, 2023
3cf1366
refactor: convert generate password script to cpp
ni-jessica Oct 21, 2023
3ddab34
feat: add error checking, create test file
ni-jessica Oct 21, 2023
19290cc
refactor: fix variable assignments
ni-jessica Oct 21, 2023
e7eedf4
Merge branch 'main' of https://github.com/csirianni/private-data-look…
ni-jessica Oct 22, 2023
1b0f7e4
feat: install catch2, add basic testing for password set creation
ni-jessica Oct 22, 2023
7288ad3
refactor: improve password generator and address misc syntax issues
ni-jessica Oct 23, 2023
c1fae6c
feat: integrating database and password
stellaljung Oct 23, 2023
e951cca
feat: refactored c++ code and integrated database
stellaljung Oct 23, 2023
748a089
chore: remove vscode file
stellaljung Oct 24, 2023
895f7fb
feat: removed unused imports and changed to snack case
stellaljung Oct 24, 2023
21c94a5
Revert "feat: removed unused imports and changed to snack case"
ni-jessica Oct 24, 2023
63ff24a
refactor: clean up imports and variable names
ni-jessica Oct 24, 2023
d966bda
docs: fix comment
csirianni Oct 24, 2023
05bde14
docs: change verb tense
csirianni Oct 24, 2023
b30c967
docs: add throws doxygen comment
csirianni Oct 24, 2023
623c50b
feat: add exception handling
csirianni Oct 24, 2023
9901f6c
test: check exception is thrown
csirianni Oct 24, 2023
b381531
build: use multiple CMakeLists.txt files
csirianni Oct 24, 2023
4ddc6ca
build: use correct library and executable names
csirianni Oct 24, 2023
015335e
refactor: rename tests folder to test
csirianni Oct 24, 2023
07beb40
refactor: rename password test file
csirianni Oct 24, 2023
0aeb3df
refactor: use member naming convention
csirianni Oct 24, 2023
95d7f6e
docs: follow markdown convention
csirianni Oct 24, 2023
ddb990a
refactor: use anonymous namespace for helper functions
csirianni Oct 24, 2023
809007e
build: use src as library name
csirianni Oct 24, 2023
7bdf526
build: add Makefile
csirianni Oct 24, 2023
efead70
ci: add backend build and test to pipeline
csirianni Oct 24, 2023
78d425b
ci: use conan profile detect
csirianni Oct 24, 2023
302714c
ci: remove conan profile update
csirianni Oct 24, 2023
12fa8b8
ci: fix build commands
csirianni Oct 24, 2023
bbf313a
ci: use correct paths
csirianni Oct 24, 2023
c5204ba
ci: use correct flag
csirianni Oct 24, 2023
f4c2064
ci: call cmake from correct location
csirianni Oct 24, 2023
9f96017
ci: put correct location on the right step
csirianni Oct 24, 2023
b95d945
ci: use correct relative path
csirianni Oct 24, 2023
fff94f3
ci: use correct relative path in test job
csirianni Oct 24, 2023
d5f7360
ci: combine build and test jobs
csirianni Oct 24, 2023
3ac850f
docs: add badge to README
csirianni Oct 24, 2023
2187b17
feat: add proper error handling to close()
csirianni Oct 24, 2023
e526754
ci: remove on pull_request from commits.yml
csirianni Oct 24, 2023
d6a09ad
docs: put badge on same line
csirianni Oct 24, 2023
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
21 changes: 21 additions & 0 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cmake_minimum_required(VERSION 3.27)

## application
project(backend)

find_package(Crow REQUIRED)
Expand All @@ -7,6 +9,7 @@ find_package(SQLite3 REQUIRED)
set(SOURCES
src/main.cpp
src/database.cpp
src/password.cpp
)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand All @@ -16,3 +19,21 @@ add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} Crow::Crow SQLite::SQLite3)

target_include_directories(${PROJECT_NAME} PRIVATE src)

## tests
ni-jessica marked this conversation as resolved.
Show resolved Hide resolved
project(tests)

find_package(Catch2 REQUIRED)

set (TEST_SOURCE
tests/testPasswordGenerator.cpp
src/password.cpp
)

add_executable(${PROJECT_NAME} ${TEST_SOURCE})

target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain)

target_include_directories(${PROJECT_NAME} PRIVATE tests)
stellaljung marked this conversation as resolved.
Show resolved Hide resolved

include(Catch)
stellaljung marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ To fix VS Code import errors with Crow, try adding the following line to your `s
```

[Source](https://stackoverflow.com/questions/58077908/linking-conan-include-to-vs-code)

## Testing
After building, you can run tests from `/backend`:
```bash
cd build && ./tests
ni-jessica marked this conversation as resolved.
Show resolved Hide resolved
```
1 change: 1 addition & 0 deletions backend/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BackendRecipe(ConanFile):
def requirements(self):
self.requires("crowcpp-crow/1.0+5")
self.requires("sqlite3/3.42.0")
self.requires("catch2/3.4.0")

def build_requirements(self):
self.tool_requires("cmake/3.22.6")
4 changes: 2 additions & 2 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <string.h>

#include "crow.h"
#include <sqlite3.h>
#include "database.hpp"
#include "password.hpp"

int main()
{
{
database::Database db = database::Database("passwords.db");
db.execute("CREATE TABLE passwords (password TEXT);");
db.execute("INSERT INTO passwords (password) VALUES ('chocolate1');");
Expand Down
75 changes: 75 additions & 0 deletions backend/src/password.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include "password.hpp"

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

if (maxChars < min_chars) {
std::cout << "Password must have a minimum length of 10 characters\n";
exit(0);
}
stellaljung marked this conversation as resolved.
Show resolved Hide resolved

std::unordered_set<std::string> passwords;
const std::string lowercase = "abcdefghijklmnopqrstuvwxyz";
const std::string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string digits = "0123456789";
const std::string symbols = "!#$%&'()\\*+,-./:;<=>?@[]^_`{|}~\"";

while (passwords.size() < numPasswords)
{
// randomize length between minChars and maxChars
stellaljung marked this conversation as resolved.
Show resolved Hide resolved
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;

while (password.length() < length)
{
int charType = rand() % 4;
int index;
switch (charType)
{
case 0: // lowercase letter
if (hasLowercase && !(hasUppercase && hasDigit && hasSymbol)){
break;
}
index = rand() % lowercase.length();
password = password + lowercase[index];
hasLowercase = true;
break;
case 1: // uppercase letter
if (hasUppercase && !(hasLowercase && hasDigit && hasSymbol)) {
break;
}
index = rand() % uppercase.length();
password = password + uppercase[index];
hasUppercase = true;
break;
case 2: // digit
if (hasDigit && !(hasLowercase && hasUppercase && hasSymbol)) {
break;
}
index = rand() % digits.length();
password = password + digits[index];
hasDigit = true;
break;
case 3: // symbol
if (hasSymbol && !(hasLowercase && hasUppercase && hasDigit)) {
break;
}
index = rand() % symbols.length();
password = password + symbols[index];
hasSymbol = true;
break;
};
};
passwords.insert(password);
};
return passwords;
};
};
19 changes: 19 additions & 0 deletions backend/src/password.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <unordered_set>
#include <string>

#ifndef PASSWORDS_H
#define PASSWORDS_H

namespace password
{
/**
* @brief Returns a set of passwords, where each password contains at least one lowercase
* letter, at least one uppercase letter, at least one digit, and at least special character.
*
* @param numPasswords The number of passwords to generate.
* @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);
}
#endif // PASSWORDS_H
29 changes: 29 additions & 0 deletions backend/tests/testPasswordGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#define CATCH_CONFIG_MAIN
#include <regex>
#include <catch2/catch_test_macros.hpp>
#include "../src/password.hpp"

bool hasLettersAndDigit(const std::string &password)
{
const std::regex pattern("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$");
return regex_match(password, pattern);
}

bool hasSymbol(const std::string &password)
{
return password.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") != std::string::npos;
}

TEST_CASE("Test generatePassword creates valid passwords")
{
std::unordered_set<std::string> passwordSet = password::generatePasswords(3, 12);
stellaljung marked this conversation as resolved.
Show resolved Hide resolved
// function generated 3 passwords
CHECK(passwordSet.size() == 3);

for (const auto &password : passwordSet)
{
CHECK(password.length() >= 10);
CHECK(hasLettersAndDigit(password));
CHECK(hasSymbol(password));
}
}