2023-08-12 22:08:52 +02:00

101 lines
1.8 KiB
C++

#include <iostream>
#include <fstream>
#include "clipboard.hpp"
struct Pass
{
char label[20];
char password[20];
Pass()
{
memset(label, 0, 20);
memset(password, 0, 20);
};
};
void load_password(char* label, char* password)
{
std::ifstream file("passwords.bin", std::ios::binary);
if (!file.is_open())
{
std::cout << "Error opening file\n";
return;
}
Pass pass;
while (file.read((char*)&pass, sizeof(Pass)))
{
if (strcmp(pass.label, label) == 0)
{
strcpy_s(password, 20, pass.password);
return;
}
}
std::cout << "Password not found\n";
file.close();
}
void save_password(char* label, char* password)
{
std::ofstream file("passwords.bin", std::ios::binary | std::ios::app);
if (!file.is_open())
{
std::cout << "Error opening file\n";
return;
}
Pass pass;
strcpy_s(pass.label, 20, label);
strcpy_s(pass.password, 20, password);
file.write((char*)&pass, sizeof(Pass));
file.close();
}
void generate_password(char* password)
{
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?";
for (int i = 0; i < 15; i++)
{
password[i] = characters[rand() % (sizeof(characters) - 1)];
}
password[15] = '\0';
}
int main(int argc, char** argv)
{
bool generate = false;
if (argc < 2)
{
std::cout << "Usage: \n";
std::cout << "Password_manager.exe <account>\n";
return 1;
}
if (!strcmp("-g", argv[1]))
{
if (argc < 3)
{
std::cout << "Usage: \n";
std::cout << "Password_manager.exe -g <account>\n";
return 1;
}
std::cout << "Generating password for " << argv[2] << "\n";
generate = true;
}
char password[20] = { 0 };
if (generate)
{
generate_password(password);
save_password(argv[2], password);
put_data_on_clipboard(password);
}
else
{
load_password(argv[1], password);
put_data_on_clipboard(password);
}
}