2023-08-13 21:15:30 +02:00

83 lines
1.5 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");
printf_s("password_manager.exe <label>\n");
printf_s("password_manager.exe -g <label>\n");
}