2023-08-26 22:10:48 +02:00

157 lines
4.2 KiB
C++

#include "func.h"
#include "glob.h"
#include "Buffer.h"
int find_password_in_buffer(Buffer& decrypted_buffer, const char* label)
{
Index* index = (Index*)decrypted_buffer.buffer;
Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index));
for (size_t i = 0; i < index->count; i++)
{
if (glob(label, (const char*) decrypted_buffer.buffer + pass[i].label + index->offset) == Glob_Result::MATCHED)
return i;
}
return -1;
}
void delete_password_from_buffer(Buffer& in, int index_of_pass)
{
Index* in_index = (Index*)in.buffer;
Pass* in_pass = (Pass*)(in.buffer + sizeof(Index));
int size_of_label = strlen((char*)in.buffer + in_pass[index_of_pass].label + in_index->offset) + 1;
int size_of_password = strlen((char*)in.buffer + in_pass[index_of_pass].password + in_index->offset) + 1;
int size_pass = size_of_label + size_of_password;
uint8_t* start_of_pass = in.buffer + in_pass[index_of_pass].label + in_index->offset;
in.remove_fast(start_of_pass, size_pass);
for (size_t i = index_of_pass; i < in_index->count; i++)
{
in_pass[i].label -= size_pass;
in_pass[i].password -= size_pass;
}
in.remove_fast((uint8_t*)& in_pass[index_of_pass], sizeof(Pass));
in_index->count -= 1;
in_index->offset -= sizeof(Pass);
}
void add_password_to_buffer(Buffer& in, const char* label, const char* password)
{
int label_start = in.add_end((uint8_t*)label, strlen(label) + 1);
int password_start = in.add_end((uint8_t*)password, strlen(password) + 1);
Index* index = (Index*)in.buffer;
label_start = label_start - index->offset;
password_start = password_start - index->offset;
Pass pass = { label_start, password_start };
index->count++;
index->offset += sizeof(Pass);
in.add_middle((uint8_t*)&pass, sizeof(Pass), index->offset - sizeof(Pass));
}
const char* get_password_from_buffer(Buffer& decrypted_buffer, int label)
{
Index* index = (Index*)decrypted_buffer.buffer;
Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index));
return (const char*)decrypted_buffer.buffer + pass[label].password + index->offset;
}
void generate_password(std::string& password, int len)
{
srand(time(NULL));
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?";
for (int i = 0; i < len; i++)
{
password += characters[rand() % (sizeof(characters) - 1)];
}
}
bool save_buffer_to_file(Buffer& buffer)
{
std::ofstream file("passwords.bin", std::ios::binary | std::ios::out);
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 | std::ios::in);
if (!file.is_open()) return false;
file.seekg(0, std::ios::end);
size_t file_size = file.tellg();
buffer.resize(file_size);
file.seekg(0, std::ios::beg);
file.read((char*)buffer.buffer, buffer.size);
if (file)
buffer.taken = file_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 print this message\n");
printf_s(" <label> \t get password of this label can use GLOB\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\n");
printf_s(" -s <label> \t show password for this label\n");
printf_s(" -l \t list all labels\n");
printf_s(" -p \t print all passwords\n");
printf_s(" -c \t change master password\n");
}
Arg get_args(int argc, char** argv, char** label) {
if (argc < 2)
{
print_usage();
return Arg::Error;
}
if (!strcmp("-h", argv[1]))
{
print_usage();
return Arg::Error;
}
if (!strcmp("-l", argv[1])) return Arg::List;
if (!strcmp("-p", argv[1])) return Arg::Print_all_p;
if (!strcmp("-c", argv[1])) return Arg::Change;
if (argc < 3)
{
*label = argv[1];
return Arg::Get;
}
*label = argv[2];
if (!strcmp("-g", argv[1])) return Arg::Generate;
if (!strcmp("-d", argv[1])) return Arg::Delete;
if (!strcmp("-i", argv[1])) return Arg::Input;
if (!strcmp("-s", argv[1])) return Arg::Show;
return Arg::Error;
}