Skip to content

Commit

Permalink
refactor: rename tables to be number only
Browse files Browse the repository at this point in the history
  • Loading branch information
ni-jessica committed Dec 11, 2023
1 parent 0299016 commit a294506
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
11 changes: 4 additions & 7 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ int main(int argc, char *argv[])
// create all tables from 0 to (2^8)^offset-1
for (int i = 0; i < std::pow(std::pow(2, 8), offset); i++)
{
std::string query_str = "table" + std::to_string(i);
db.execute("CREATE TABLE " + query_str + " (password TEXT);");
spdlog::info("Created table: {}", query_str);
db.execute("CREATE TABLE `" + std::to_string(i) + "` (password TEXT);");
}

// generate and insert the passwords into the database
Expand All @@ -81,11 +79,10 @@ int main(int argc, char *argv[])
{
// determine which table to insert into based on leaked byte
std::string encoded_byte = crow::utility::base64encode(password.substr(0, offset), offset);
std::string table_num = "table" + std::to_string(static_cast<unsigned int>(encoded_byte[0]));

std::string table_num = std::to_string(static_cast<unsigned int>(encoded_byte[0]));
std::string raw_password = password.substr(offset, password.size() - offset);
// encode password before inserting into database
db.execute("INSERT INTO " + table_num + " (password) VALUES ('" + crow::utility::base64encode(raw_password, raw_password.size()) + "');");
db.execute("INSERT INTO `" + table_num + "` (password) VALUES ('" + crow::utility::base64encode(raw_password, raw_password.size()) + "');");
}

// create key table
Expand All @@ -106,7 +103,7 @@ int main(int argc, char *argv[])
return count;
};

// check if key ad offset table exists
// check if key and offset table exists
std::vector<bool> secret_key = db.execute("SELECT COUNT(*) FROM sqlite_schema WHERE name = 'secret';", callback);
std::vector<bool> offset_key = db.execute("SELECT COUNT(*) FROM sqlite_schema WHERE name = 'offset';", callback);
if (secret_key.front() == 0 || offset_key.front() == 0) // no secret key or no offset key table exists
Expand Down
6 changes: 3 additions & 3 deletions backend/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace server
// get the table num corresponding to the user password leaked byte
std::string decoded_password = crow::utility::base64decode(user_password, user_password.size());
std::string encoded_leaked_byte = crow::utility::base64encode(decoded_password.substr(0, offset), offset);
std::string table_num = "table" + std::to_string(static_cast<unsigned int>(encoded_leaked_byte[0]));
std::string table_num = std::to_string(static_cast<unsigned int>(encoded_leaked_byte[0]));

// get all passwords from the table corresponding to the user password leaked byte
std::vector<std::string> breached_passwords = db.execute("SELECT * FROM " + table_num + ";", callback);
std::vector<std::string> breached_passwords = db.execute("SELECT * FROM `" + table_num + "`;", callback);

// get b secret key from database
std::string encoded_b = db.execute("SELECT * FROM secret;", callback)[0];
Expand All @@ -49,7 +49,7 @@ namespace server
unsigned char *b = (unsigned char *)decoded_b.data();

// encrypt user password
std::string encrypted_password = cryptography::encryptPassword(crow::utility::base64decode(user_password, user_password.size()), b, offset);
std::string encrypted_password = cryptography::encryptPassword(decoded_password, b, offset);
response["status"] = "success";
response["userPassword"] = crow::utility::base64encode(encrypted_password, encrypted_password.size());
response["breachedPasswords"] = breached_passwords;
Expand Down
12 changes: 4 additions & 8 deletions backend/tests/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ TEST_CASE("Test endpoints using handler")
database::Database db = database::Database(path, true);

// offset constant
int offset = 1;
const size_t offset = 1;

// create all tables from 0 to (2^8)^offset-1
for (int i = 0; i < std::pow(std::pow(2, 8), offset); i++)
{
std::string query_str = "table" + std::to_string(i);
db.execute("CREATE TABLE " + query_str + " (password TEXT);");
spdlog::info("Table Created: {}", query_str);
db.execute("CREATE TABLE `" + std::to_string(i) + "` (password TEXT);");
}

// create a mock password set
Expand All @@ -54,14 +52,12 @@ TEST_CASE("Test endpoints using handler")
{
// determine which table to insert into based on leaked byte
std::string encoded_byte = crow::utility::base64encode(password.substr(0, offset), offset);
unsigned int table_num = static_cast<int>(encoded_byte[0]);
std::string table_str = std::to_string(table_num);
std::string query_str = "table" + table_str;
std::string table_num = std::to_string(static_cast<unsigned int>(encoded_byte[0]));

std::string raw_password = password.substr(offset, password.size() - offset);

// encode password before inserting into database
db.execute("INSERT INTO " + query_str + " (password) VALUES ('" + crow::utility::base64encode(raw_password, raw_password.size()) + "');");
db.execute("INSERT INTO `" + table_num + "` (password) VALUES ('" + crow::utility::base64encode(raw_password, raw_password.size()) + "');");
}

// create key table
Expand Down

0 comments on commit a294506

Please sign in to comment.