120 lines
2.5 KiB
C++
120 lines
2.5 KiB
C++
#include "func.h"
|
|
|
|
Pass* find_password(Buffer* buff, char* label)
|
|
{
|
|
Pass* passwords = (Pass*)buff->buffer;
|
|
|
|
for (int i = 0; i < buff->taken / sizeof(Pass); i++)
|
|
{
|
|
if (!strcmp(passwords[i].label, label))
|
|
{
|
|
return &passwords[i];
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void generate_password(char* password)
|
|
{
|
|
srand(time(NULL));
|
|
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?";
|
|
for (int i = 0; i < 15; i++)
|
|
{
|
|
password[i] = characters[rand() % (sizeof(characters) - 1)];
|
|
}
|
|
password[15] = '\0';
|
|
}
|
|
|
|
bool save_buffer_to_file(Buffer* buffer)
|
|
{
|
|
std::ofstream file("passwords.bin", std::ios::binary);
|
|
if (!file.is_open())
|
|
{
|
|
printf_s("Error saving file\n");
|
|
return false;
|
|
}
|
|
file.write((char*)buffer->buffer, buffer->taken);
|
|
file.close();
|
|
return true;
|
|
}
|
|
|
|
bool load_buffer_from_file(Buffer* buffer)
|
|
{
|
|
|
|
std::ifstream file("passwords.bin", std::ios::binary);
|
|
if (!file.is_open()) return false;
|
|
|
|
file.seekg(0, std::ios::end);
|
|
buffer->resize(file.tellg());
|
|
file.seekg(0, std::ios::beg);
|
|
file.read((char*)buffer->buffer, buffer->size);
|
|
|
|
if (file)
|
|
buffer->taken = buffer->size;
|
|
else
|
|
buffer->taken = file.gcount();
|
|
|
|
file.close();
|
|
return true;
|
|
}
|
|
|
|
|
|
void print_usage()
|
|
{
|
|
printf_s(" Usage:\n\n");
|
|
printf_s(" password_manager.exe [flags]\n\n");
|
|
printf_s(" Note: <label> max len %d char\n\n", MAX_STRING_SIZE);
|
|
printf_s(" Flags:\n\n");
|
|
printf_s(" -h \t print this message\n");
|
|
printf_s(" <label> \t get password of this label\n");
|
|
printf_s(" -g <label> \t generate or update password of this label\n");
|
|
printf_s(" -d <label> \t delete password of this label\n");
|
|
printf_s(" -i <label> \t input password for this label max %d char\n", MAX_STRING_SIZE);
|
|
printf_s(" -l \t list all labels\n");
|
|
printf_s(" -p \t print all passwords\n");
|
|
}
|
|
|
|
Args get_args(int argc, char** argv, char** label) {
|
|
if (argc < 2)
|
|
{
|
|
print_usage();
|
|
return Args::Error;
|
|
}
|
|
|
|
if (!strcmp("-h", argv[1]))
|
|
{
|
|
print_usage();
|
|
return Args::Error;
|
|
}
|
|
|
|
if (!strcmp("-l", argv[1])) return Args::List;
|
|
if (!strcmp("-p", argv[1])) return Args::Print_all_p;
|
|
|
|
if (argc < 3)
|
|
{
|
|
*label = argv[1];
|
|
return Args::Get;
|
|
}
|
|
|
|
*label = argv[2];
|
|
|
|
if (!strcmp("-g", argv[1]))
|
|
{
|
|
printf_s("Generating password for %s\n", argv[2]);
|
|
return Args::Generate;
|
|
}
|
|
|
|
if (!strcmp("-d", argv[1]))
|
|
{
|
|
printf_s("Deleting password for %s\n", argv[2]);
|
|
return Args::Delete;
|
|
}
|
|
|
|
if (!strcmp("-i", argv[1]))
|
|
{
|
|
return Args::Input;
|
|
}
|
|
|
|
|
|
return Args::Error;
|
|
} |