Skip to content

Commit

Permalink
refactor: use static cast for table_num
Browse files Browse the repository at this point in the history
  • Loading branch information
csirianni committed Dec 12, 2023
1 parent bc62166 commit e3c421d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 4 additions & 2 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ int main(int argc, char *argv[])
for (const auto &password : encrypted_passwords)
{
// determine which table to insert into based on leaked byte
unsigned int leaked_byte = ((unsigned char)password.substr(0, offset)[0]) & (num_tables-1);
// unsigned char cast is required to prevent unintentional sign extension
// TODO: support more than one byte using helper function
unsigned int table_num = static_cast<unsigned int>(static_cast<unsigned char>(password.substr(0, offset)[0]));
std::string raw_password = password.substr(offset, password.size() - offset);
// encode password before inserting into database
db.execute("INSERT INTO `" + std::to_string(leaked_byte) + "` (password) VALUES ('" + crow::utility::base64encode(raw_password, raw_password.size()) + "');");
db.execute("INSERT INTO `" + std::to_string(table_num) + "` (password) VALUES ('" + crow::utility::base64encode(raw_password, raw_password.size()) + "');");
}

// create key table
Expand Down
3 changes: 1 addition & 2 deletions backend/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ 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());
unsigned char leaked_byte = decoded_password.substr(0, offset)[0];
unsigned int table_num = leaked_byte & ((int)std::pow(std::pow(2, 8), offset) - 1);
unsigned int table_num = static_cast<unsigned int>(static_cast<unsigned char>(decoded_password.substr(0, offset)[0]));

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

0 comments on commit e3c421d

Please sign in to comment.