#include "arg_func.h" #include "func.h" #include #include "win.h" #include "func.h" #include "buffer.h" #include "cryptography.h" const char*arg_get(Buffer& decrypted_buffer, const char* label) { int pass = find_password_in_buffer(decrypted_buffer, label); if (pass < 0) { printf_s("Password not found\n"); return nullptr; } return get_password_from_buffer(decrypted_buffer, pass); } 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); 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; } 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"); return nullptr; } int pass = find_password_in_buffer(decrypted_buffer, label); 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; } void arg_list(Buffer& decrypted_buffer) { Index* index = (Index*)decrypted_buffer.buffer; Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index)); for (size_t i = 0; i < index->count; i++) { printf_s("label: %s\n", decrypted_buffer.buffer + pass[i].label + index->offset); } } void arg_delete(Buffer& in_buffer, Buffer& out_buffer, const char* label_to_del, Cryptography& crypto) { printf_s("Deleting password for %s\n", label_to_del); int pass = find_password_in_buffer(in_buffer, label_to_del); if (pass < 0) { printf_s("Password not found\n"); return; } delete_password_from_buffer(in_buffer, out_buffer, pass); crypto.encrypt(&out_buffer, &in_buffer); save_buffer_to_file(in_buffer); printf_s("Password deleted\n"); } void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass) { printf_s("Input main password for confirmation:"); std::string new_string = get_user_password(); if (new_string.empty()) { printf_s("Error getting password\n"); return; } if (new_string != user_pass) { printf_s("Wrong password\n"); return; } Index* index = (Index*)decrypted_buffer.buffer; Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index)); for (size_t i = 0; i < index->count; i++) { printf_s("label: %s\t\t\t", decrypted_buffer.buffer + pass[i].label + index->offset); printf_s("password: %s\n", decrypted_buffer.buffer + pass[i].password + index->offset); } } void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& user_pass, Cryptography& crypto) { printf_s("Input main password for confirmation:"); std::string new_string = get_user_password(); if (new_string.empty()) { printf_s("Error getting password\n"); return; } if (new_string != user_pass) { printf_s("Passwords don't match\n"); return; } printf_s("Input new password:"); new_string = get_user_password(); if (new_string.empty()) { printf_s("Error getting password\n"); return; } printf_s("Input new password again:"); user_pass = get_user_password(); if (user_pass.empty()) { printf_s("Error getting password\n"); return; } if (new_string != user_pass) { printf_s("Passwords don't match\n"); return; } crypto.generate_key_and_iv_from_password(new_string.c_str(), new_string.size()); crypto.encrypt(&decrypted_buffer, &encrypted_buffer); save_buffer_to_file(encrypted_buffer); printf_s("Password changed\n"); } void arg_show(Buffer& decrypted_buffer, const char* label) { int pass = find_password_in_buffer(decrypted_buffer, label); if (pass < 0) printf_s("Password not found\n"); else printf_s("Password: %s\n", get_password_from_buffer(decrypted_buffer, pass)); }