2023-08-24 22:26:41 +02:00

269 lines
5.9 KiB
C++

#include <iostream>
#include <fstream>
#include "win.h"
#include "buffer.h"
#include "cryptography.h"
#include "func.h"
Pass* arg_get(Buffer& decrypted_buffer, char* label)
{
Pass* pass = find_password(&decrypted_buffer, label);
if (!pass) printf_s("Password not found\n");
return pass;
}
Pass* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, char* label, Cryptography& crypto)
{
printf_s("Generating password for %s\n", label);
Pass* pass = find_password(&decrypted_buffer, label);
Pass new_pass = { 0 };
if (pass != nullptr) generate_password(pass->password);
else
{
generate_password(new_pass.password);
strcpy_s(new_pass.label, MAX_STRING_SIZE, label);
int position = decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass));
if (position < 0)
{
printf_s("Error adding password\n");
return nullptr;
}
pass = (Pass*)decrypted_buffer.buffer + position;
}
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
save_buffer_to_file(&encrypted_buffer);
return pass;
}
void arg_list(Buffer& decrypted_buffer)
{
Pass* passwords = (Pass*)decrypted_buffer.buffer;
for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++)
{
printf_s("%s\n", passwords[i].label);
}
}
void arg_delete(Buffer& decrypted_buffer, Buffer& encrypted_buffer, char* label, Cryptography& crypto)
{
printf_s("Deleting password for %s\n", label);
Pass* pass = find_password(&decrypted_buffer, label);
if (!pass)
{
printf_s("Password not found\n");
return;
}
else
{
decrypted_buffer.remove_fast((uint8_t*)pass, sizeof(Pass));
}
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
save_buffer_to_file(&encrypted_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:\n");
std::string new_string = get_passwd();
if (new_string.empty())
{
printf_s("Error getting password\n");
return;
}
if (new_string != user_pass)
{
printf_s("Wrong password\n");
return;
}
Pass* passwords = (Pass*)decrypted_buffer.buffer;
for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++)
{
printf_s("%s: %s\n", passwords[i].label, passwords[i].password);
}
}
Pass* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, char* label, Cryptography& crypto)
{
printf_s("Input password for %s:\n", label);
std::string new_string = get_passwd();
if (new_string.empty())
{
printf_s("Error getting password\n");
return nullptr;
}
Pass* pass = find_password(&decrypted_buffer, label);
Pass new_pass = { 0 };
if (pass != nullptr)
{
strcpy_s(pass->password, MAX_STRING_SIZE, new_string.c_str());
}
else
{
strcpy_s(new_pass.password, MAX_STRING_SIZE, new_string.c_str());
strcpy_s(new_pass.label, MAX_STRING_SIZE, label);
int position = decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass));
if (position < 0)
{
printf_s("Error adding password\n");
return nullptr;
}
pass = (Pass*) decrypted_buffer.buffer + position;
}
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
save_buffer_to_file(&encrypted_buffer);
return pass;
}
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_passwd();
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_passwd();
if (new_string.empty())
{
printf_s("Error getting password\n");
return;
}
printf_s("Input new password again:");
user_pass = get_passwd();
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, char* label) {
Pass* pass = find_password(&decrypted_buffer, label);
if (!pass)
printf_s("Password not found\n");
else
printf_s("Password: %s\n", pass->password);
}
int main(int argc, char** argv)
{
char* label = nullptr;
Arg args = get_args(argc, argv, &label);
if (args == Arg::Error) return 1;
Buffer encrypted_buffer;
if (!load_buffer_from_file(&encrypted_buffer))
if (!(args == Arg::Generate)) {
printf_s("No passwords, try generating password\n");
return 1;
}
printf_s("Input main password:");
std::string user_pass = get_passwd();
if (user_pass.empty())
{
printf_s("Error getting password\n");
return 1;
}
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;
Pass* pass = nullptr;
switch (args)
{
case Arg::Get:
pass = arg_get(decrypted_buffer, label);
if (!pass) return 1;
break;
case Arg::Generate:
pass = arg_generate(decrypted_buffer, encrypted_buffer, label, crypto);
if (!pass) return 1;
break;
case Arg::List:
arg_list(decrypted_buffer);
return 0;
break;
case Arg::Delete:
arg_delete(decrypted_buffer, encrypted_buffer, label, crypto);
return 0;
break;
case Arg::Print_all_p:
arg_print_all_p(decrypted_buffer, user_pass);
return 0;
break;
case Arg::Input:
pass = arg_input (decrypted_buffer, encrypted_buffer, label, crypto);
if (!pass) return 1;
break;
case Arg::Change:
arg_change(decrypted_buffer, encrypted_buffer, user_pass, crypto);
return 0;
case Arg::Show:
arg_show(decrypted_buffer, label);
return 0;
break;
}
if (pass == nullptr) return 1;
#ifdef _DEBUG
printf_s("Password: %s", pass->password);
#else
printf_s("Password copied to clipboard\n");
put_data_on_clipboard(pass->password);
#endif
}