2023-08-13 22:09:08 +02:00

91 lines
1.9 KiB
C++

#pragma once
#include <fstream>
#include "buffer.h"
struct Pass
{
char label[20];
char password[20];
Pass()
{
memset(label, 0, 20);
memset(password, 0, 20);
};
};
bool find_password(Buffer* buff, char* label, char* password)
{
Pass* passwords = (Pass*)buff->buffer;
for (int i = 0; i < buff->taken / sizeof(Pass); i++)
{
if (!strcmp(passwords[i].label, label))
{
strcpy_s(password, 20, passwords[i].password);
return true;
}
}
return false;
}
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(" Flags:\n\n");
printf_s(" -h\t\t\tprint this message WIP\n");
printf_s(" <label>\t\tget password of this label\n");
printf_s(" -g <label>\t\tgenerate or update password of this label WIP [update]\n");
printf_s(" -l\t\t\tlist all labels WIP\n");
printf_s(" -d <label>\t\tdelete password of this label WIP \n");
printf_s(" -c\t\t\tclear all passwords WIP\n");
printf_s(" -p\t\t\tprint all passwords WIP\n");
}