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 all 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
41 changes: 41 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Backend

on: [pull_request, push]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
test:
# The CMake configure and build commands are platform agnostic and should work equally
# well on Windows or Mac. You can convert this to a matrix build if you need
# cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Get Conan
uses: turtlebrowser/get-conan@v1.0

- name: Create default profile
working-directory: ./backend
run: conan profile detect

- name: Install dependencies
working-directory: ${{github.workspace}}/backend
run: conan install . --output-folder=build --build=missing -s build_type=${{env.BUILD_TYPE}}

- name: Configure CMake
working-directory: ${{github.workspace}}/backend/build
run: cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
working-directory: ${{github.workspace}}/backend/build
run: cmake --build . --config ${{env.BUILD_TYPE}}

- name: Run tests
working-directory: ${{github.workspace}}/backend/build
run: ./test/pdl_test
2 changes: 1 addition & 1 deletion .github/workflows/commits.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Conventional Commits

on: [pull_request, push]
on: [push]

jobs:
build:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
.vscode
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Private Data Lookup

[![Conventional Commits](https://github.com/csirianni/private-data-lookup/actions/workflows/commits.yml/badge.svg)](https://github.com/csirianni/private-data-lookup/actions/workflows/commits.yml)
[![Backend](https://github.com/csirianni/private-data-lookup/actions/workflows/backend.yml/badge.svg)](https://github.com/csirianni/private-data-lookup/actions/workflows/backend.yml)

## Overview

Expand Down
21 changes: 9 additions & 12 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
cmake_minimum_required(VERSION 3.27)
project(backend)

find_package(Crow REQUIRED)
find_package(SQLite3 REQUIRED)
project(pdl LANGUAGES CXX)

set(SOURCES
src/main.cpp
src/database.cpp
)
add_subdirectory("src")
add_subdirectory("test")

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(${PROJECT_NAME} ${SOURCES})
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD_20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
endif()

target_link_libraries(${PROJECT_NAME} Crow::Crow SQLite::SQLite3)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

target_include_directories(${PROJECT_NAME} PRIVATE src)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address,undefined,float-divide-by-zero,float-cast-overflow, -fno-sanitize=all,null,alignment")
15 changes: 15 additions & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BUILD_DIR = build

all: build

build:
cd $(BUILD_DIR) && cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cd $(BUILD_DIR) && cmake --build .

run: build
cd ${BUILD_DIR} && ./src/server

check: build
cd $(BUILD_DIR) && ./test/pdl_test

.PHONY: all build run check
8 changes: 8 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ 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")
10 changes: 10 additions & 0 deletions backend/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
find_package(Crow REQUIRED)
find_package(SQLite3 REQUIRED)

add_library(src database.cpp password.cpp)

add_executable(server main.cpp)

target_link_libraries(server Crow::Crow SQLite::SQLite3 src)

target_include_directories(server PRIVATE src)
29 changes: 17 additions & 12 deletions backend/src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,61 @@ namespace

namespace database
{
Database::Database(const std::string &file_path) : is_closed(false)
Database::Database(const std::string &file_path) : is_closed_(false)
{
if (std::filesystem::exists(file_path))
{
std::remove(file_path.c_str());
}

int result = sqlite3_open_v2(file_path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
int result = sqlite3_open_v2(file_path.c_str(), &db_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
const char *error_msg = sqlite3_errmsg(db_);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

void Database::execute(const std::string &command)
{
int result = sqlite3_exec(db, command.c_str(), NULL, NULL, NULL);
int result = sqlite3_exec(db_, command.c_str(), NULL, NULL, NULL);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
const char *error_msg = sqlite3_errmsg(db_);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

void Database::printTable(const std::string &table)
{
const std::string query = "SELECT * FROM " + table + ";";
int result = sqlite3_exec(db, query.c_str(), printRow, NULL, NULL);
int result = sqlite3_exec(db_, query.c_str(), printRow, NULL, NULL);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
const char *error_msg = sqlite3_errmsg(db_);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
}

void Database::close()
{
sqlite3_close(db);
is_closed = true;
int result = sqlite3_close(db_);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db_);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
is_closed_ = true;
}

Database::~Database()
{
if (!is_closed)
if (!is_closed_)
{
int result = sqlite3_close(db);
int result = sqlite3_close(db_);
if (result != SQLITE_OK)
{
const char *error_msg = sqlite3_errmsg(db);
const char *error_msg = sqlite3_errmsg(db_);
fprintf(stderr, "SQLite error: %s\n", error_msg);
}
std::abort();
Expand Down
4 changes: 2 additions & 2 deletions backend/src/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ namespace database
~Database();

private:
sqlite3 *db;
bool is_closed;
sqlite3 *db_;
bool is_closed_;
};
}
#endif // DATABASE_H
10 changes: 8 additions & 2 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include <stdlib.h>
#include <stdio.h>
#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);");

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

Expand Down
80 changes: 80 additions & 0 deletions backend/src/password.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include "password.hpp"

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

// ensure max_chars is greater than min_chars
if (max_chars < min_chars)
{
throw std::invalid_argument("max_chars must be > " + std::to_string(min_chars));
}
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() < num_passwords)
{
// randomize length between min_chars and max_chars
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 char_type = rand() % 4;
int index;
switch (char_type)
{
case 0: // lowercase letter
if (has_lowercase && !(has_uppercase && has_digit && has_symbol))
Copy link
Owner

@csirianni csirianni Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: I like this condition! I get it now.

{
break;
}
index = rand() % lowercase.length();
password = password + lowercase[index];
has_lowercase = true;
break;
case 1: // uppercase letter
if (has_uppercase && !(has_lowercase && has_digit && has_symbol))
{
break;
}
index = rand() % uppercase.length();
password = password + uppercase[index];
has_uppercase = true;
break;
case 2: // digit
if (has_digit && !(has_lowercase && has_uppercase && has_symbol))
{
break;
}
index = rand() % digits.length();
password = password + digits[index];
has_digit = true;
break;
case 3: // symbol
if (has_symbol && !(has_lowercase && has_uppercase && has_digit))
{
break;
}
index = rand() % symbols.length();
password = password + symbols[index];
has_symbol = true;
break;
};
};
passwords.insert(password);
};
return passwords;
};
};
20 changes: 20 additions & 0 deletions backend/src/password.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <unordered_set>
#include <string>

#ifndef PASSWORDS_H
#define PASSWORDS_H

namespace password
{
/**
* @brief Generate 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 num_passwords The number of passwords to generate.
* @param max_chars The maximum number of characters for a password.
* @return std::unordered_set<std::string> The passwords generated.
* @throws std::invalid_argument If max_chars is < min_chars (locally defined).
*/
std::unordered_set<std::string> generatePasswords(int num_passwords, int max_chars);
}
#endif // PASSWORDS_H
11 changes: 11 additions & 0 deletions backend/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
find_package(Catch2 REQUIRED)

set (TEST_SOURCE
password.cpp
)

add_executable(pdl_test ${TEST_SOURCE})

target_link_libraries(pdl_test src Catch2::Catch2WithMain)

target_include_directories(pdl_test PUBLIC ${CMAKE_SOURCE_DIR}/src)
Loading
Loading