From 30a99f35a6594447b29582b8559ac492d8b7f76e Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Sat, 26 Aug 2023 22:10:48 +0200 Subject: [PATCH] Optimize delete password from buffer --- Password_manager/include/func.h | 2 +- Password_manager/source/arg_func.cpp | 88 +++++----------------------- Password_manager/source/func.cpp | 37 +++++------- Password_manager/source/main.cpp | 13 +++- 4 files changed, 43 insertions(+), 97 deletions(-) diff --git a/Password_manager/include/func.h b/Password_manager/include/func.h index 355ea4c..43f29d6 100644 --- a/Password_manager/include/func.h +++ b/Password_manager/include/func.h @@ -30,7 +30,7 @@ enum class Arg int find_password_in_buffer(Buffer& buff, const char* label); -void delete_password_from_buffer(Buffer& in, Buffer& out, int index_of_pass); +void delete_password_from_buffer(Buffer& in, int index_of_pass); void add_password_to_buffer(Buffer& in, const char* label, const char* password); diff --git a/Password_manager/source/arg_func.cpp b/Password_manager/source/arg_func.cpp index 2ec8f88..9bf58b5 100644 --- a/Password_manager/source/arg_func.cpp +++ b/Password_manager/source/arg_func.cpp @@ -20,61 +20,26 @@ const char*arg_get(Buffer& decrypted_buffer, const char* label) const char* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) { - if (decrypted_buffer.taken < 8) { - Index index = { 0, 0 }; - index.offset = sizeof(Index); - decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); - } - printf_s("Generating password for %s\n", label); - int pass = find_password_in_buffer(decrypted_buffer, label); - std::string password; - generate_password(password, 15); + int pass = find_password_in_buffer(decrypted_buffer, label); + if (pass >= 0) delete_password_from_buffer(decrypted_buffer, pass); - if (pass < 0) - { - add_password_to_buffer(decrypted_buffer, label, password.c_str()); - crypto.encrypt(&decrypted_buffer, &encrypted_buffer); - save_buffer_to_file(encrypted_buffer); - - Index* index = (Index*)decrypted_buffer.buffer; - - return get_password_from_buffer(decrypted_buffer, index->count - 1); - - } - else - { - - delete_password_from_buffer(decrypted_buffer, encrypted_buffer, pass); - add_password_to_buffer(encrypted_buffer, label, password.c_str()); - crypto.encrypt(&encrypted_buffer, &decrypted_buffer); - save_buffer_to_file(decrypted_buffer); - - Index* index = (Index*)encrypted_buffer.buffer; - - return get_password_from_buffer(encrypted_buffer, index->count - 1); - - } - - return 0; + add_password_to_buffer(decrypted_buffer, label, password.c_str()); + crypto.encrypt(&decrypted_buffer, &encrypted_buffer); + save_buffer_to_file(encrypted_buffer); + Index* index = (Index*)decrypted_buffer.buffer; + return get_password_from_buffer(decrypted_buffer, index->count - 1); } const char* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto) { - if (decrypted_buffer.taken < 8) { - Index index = { 0, 0 }; - index.offset = sizeof(Index); - decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); - } - printf_s("Input password for %s:", label); std::string new_string = get_user_password(); - if (new_string.empty()) { printf_s("error getting password\n"); @@ -82,34 +47,13 @@ const char* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const } int pass = find_password_in_buffer(decrypted_buffer, label); + if (pass >= 0) delete_password_from_buffer(decrypted_buffer, pass); - if (pass < 0) - { - add_password_to_buffer(decrypted_buffer, label, new_string.c_str()); - crypto.encrypt(&decrypted_buffer, &encrypted_buffer); - save_buffer_to_file(encrypted_buffer); - - Index* index = (Index*)decrypted_buffer.buffer; - - return get_password_from_buffer(decrypted_buffer, index->count - 1); - - - } - else - { - delete_password_from_buffer(decrypted_buffer, encrypted_buffer, pass); - add_password_to_buffer(encrypted_buffer, label, new_string.c_str()); - - crypto.encrypt(&encrypted_buffer, &decrypted_buffer); - save_buffer_to_file(decrypted_buffer); - - Index* index = (Index*)encrypted_buffer.buffer; - - return get_password_from_buffer(encrypted_buffer, index->count - 1); - - } - - return 0; + add_password_to_buffer(decrypted_buffer, label, new_string.c_str()); + crypto.encrypt(&decrypted_buffer, &encrypted_buffer); + save_buffer_to_file(encrypted_buffer); + Index* index = (Index*)decrypted_buffer.buffer; + return get_password_from_buffer(decrypted_buffer, index->count - 1); } void arg_list(Buffer& decrypted_buffer) @@ -133,10 +77,10 @@ void arg_delete(Buffer& in_buffer, Buffer& out_buffer, const char* label_to_del, return; } - delete_password_from_buffer(in_buffer, out_buffer, pass); + delete_password_from_buffer(in_buffer, pass); - crypto.encrypt(&out_buffer, &in_buffer); - save_buffer_to_file(in_buffer); + crypto.encrypt(&in_buffer, &out_buffer); + save_buffer_to_file(out_buffer); printf_s("Password deleted\n"); } diff --git a/Password_manager/source/func.cpp b/Password_manager/source/func.cpp index a5fa389..9f3a290 100644 --- a/Password_manager/source/func.cpp +++ b/Password_manager/source/func.cpp @@ -16,36 +16,27 @@ int find_password_in_buffer(Buffer& decrypted_buffer, const char* label) return -1; } -void delete_password_from_buffer(Buffer& in, Buffer& out, int index_of_pass) +void delete_password_from_buffer(Buffer& in, int index_of_pass) { Index* in_index = (Index*)in.buffer; Pass* in_pass = (Pass*)(in.buffer + sizeof(Index)); - int size = sizeof(Index) + sizeof(Pass) * (in_index->count - 1); - out.resize(size); - out.taken = size; + int size_of_label = strlen((char*)in.buffer + in_pass[index_of_pass].label + in_index->offset) + 1; + int size_of_password = strlen((char*)in.buffer + in_pass[index_of_pass].password + in_index->offset) + 1; + int size_pass = size_of_label + size_of_password; - Index* out_index = (Index*)out.buffer; - out_index->count = in_index->count - 1; - out_index->offset = size; + uint8_t* start_of_pass = in.buffer + in_pass[index_of_pass].label + in_index->offset; + in.remove_fast(start_of_pass, size_pass); - - int count = 0; - for (size_t i = 0; i < in_index->count; i++) + for (size_t i = index_of_pass; i < in_index->count; i++) { - if (i == index_of_pass) continue; - - uint8_t* label = in.buffer + in_pass[i].label + in_index->offset; - uint8_t* password = in.buffer + in_pass[i].password + in_index->offset; - - size_t label_start = out.add_end(label, strlen((char*)label) + 1); - size_t password_start = out.add_end(password, strlen((char*)password) + 1); - - Pass* out_pass = (Pass*)(out.buffer + sizeof(Index)); - out_pass[count].label = label_start - size; - out_pass[count].password = password_start - size; - count++; + in_pass[i].label -= size_pass; + in_pass[i].password -= size_pass; } + + in.remove_fast((uint8_t*)& in_pass[index_of_pass], sizeof(Pass)); + in_index->count -= 1; + in_index->offset -= sizeof(Pass); } void add_password_to_buffer(Buffer& in, const char* label, const char* password) @@ -78,7 +69,7 @@ void generate_password(std::string& password, int len) char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?"; for (int i = 0; i < len; i++) { - password[i] += characters[rand() % (sizeof(characters) - 1)]; + password += characters[rand() % (sizeof(characters) - 1)]; } } diff --git a/Password_manager/source/main.cpp b/Password_manager/source/main.cpp index 5e680ea..51894f8 100644 --- a/Password_manager/source/main.cpp +++ b/Password_manager/source/main.cpp @@ -23,13 +23,18 @@ int main() return 1; } - std::string user_pass = "123"; Cryptography crypto(user_pass.c_str(), user_pass.size()); Buffer decrypted_buffer; if (encrypted_buffer.size > 0) if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1; + if (decrypted_buffer.taken < 8) { + Index index = { 0, 0 }; + index.offset = sizeof(Index); + decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); + } + const char* pass = nullptr; switch (args) @@ -103,6 +108,12 @@ int main(int argc, char** argv) if (encrypted_buffer.size > 0) if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1; + if (decrypted_buffer.taken < 8) { + Index index = { 0, 0 }; + index.offset = sizeof(Index); + decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); + } + const char* pass = nullptr; switch (args)