Rename Pass to LoginInfo

and move print_usage, get_args to arg_func.h
This commit is contained in:
Nikola Petrov 2023-08-27 20:16:37 +02:00
parent 746859c70e
commit a900289dbf
4 changed files with 111 additions and 112 deletions

View File

@ -5,6 +5,23 @@ class Buffer;
class Cryptography;
#include <string>
enum class Arg
{
Get, // get password for label
Generate, // generate password for label
List, // list all labels
Delete, // delete password for label
Print_all_p, // print all passwords
Input, // input password for label
Change, // change main password
Show, // show password for label
Error // error
};
void print_args();
Arg get_args(int argc, char** argv, char** label);
const char* arg_get(Buffer& decrypted_buffer, const char* label);
const char* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto);

View File

@ -3,10 +3,10 @@
class Buffer;
struct Pass
struct LoginInfo
{
uint32_t label;
uint32_t password;
uint32_t password;
};
struct Index
@ -15,26 +15,13 @@ struct Index
uint32_t offset;
};
enum class Arg
{
Get, // get password for label
Generate, // generate password for label
List, // list all labels
Delete, // delete password for label
Print_all_p, // print all passwords
Input, // input password for label
Change, // change main password
Show, // show password for label
Error // error
};
int find_logininfo_in_buffer(Buffer& buffer, const char* label);
int find_password_in_buffer(Buffer& buff, const char* label);
void delete_logininfo_from_buffer(Buffer& buffer, int index_of_pass);
void delete_password_from_buffer(Buffer& in, int index_of_pass);
void add_logininfo_to_buffer(Buffer& buffer, const char* label, const char* password);
void add_password_to_buffer(Buffer& in, const char* label, const char* password);
const char* get_password_from_buffer(Buffer& decrypted_buffer, int label);
const char* get_logininfo_pointer_from_buffer(Buffer& buffer, int index_of_pass);
void generate_password(std::string& password, int len);
@ -42,6 +29,3 @@ bool save_buffer_to_file(Buffer& buffer);
bool load_buffer_from_file(Buffer& buffer);
void print_usage();
Arg get_args(int argc, char** argv, char** label);

View File

@ -7,15 +7,63 @@
#include "buffer.h"
#include "cryptography.h"
void print_args()
{
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 password of this label (or update if exists)\n");
printf_s(" -d <label> \t delete password of this label\n");
printf_s(" -i <label> \t input password for this label (or update if exists)\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_args();
return Arg::Error;
}
if (!strcmp("-h", argv[1]))
{
print_args();
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;
}
const char* arg_get(Buffer& decrypted_buffer, const char* label)
{
int pass = find_password_in_buffer(decrypted_buffer, label);
int pass = find_logininfo_in_buffer(decrypted_buffer, label);
if (pass < 0) {
printf_s("Password not found\n");
return nullptr;
}
return get_password_from_buffer(decrypted_buffer, pass);
return get_logininfo_pointer_from_buffer(decrypted_buffer, pass);
}
const char* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto)
@ -25,14 +73,14 @@ const char* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, con
std::string password;
generate_password(password, 15);
int pass = find_password_in_buffer(decrypted_buffer, label);
if (pass >= 0) delete_password_from_buffer(decrypted_buffer, pass);
int pass = find_logininfo_in_buffer(decrypted_buffer, label);
if (pass >= 0) delete_logininfo_from_buffer(decrypted_buffer, pass);
add_password_to_buffer(decrypted_buffer, label, password.c_str());
add_logininfo_to_buffer(decrypted_buffer, label, password.c_str());
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
save_buffer_to_file(encrypted_buffer);
Index* index = (Index*)decrypted_buffer.buffer;
return get_password_from_buffer(decrypted_buffer, index->count - 1);
return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1);
}
const char* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto)
@ -46,20 +94,20 @@ const char* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const
return nullptr;
}
int pass = find_password_in_buffer(decrypted_buffer, label);
if (pass >= 0) delete_password_from_buffer(decrypted_buffer, pass);
int pass = find_logininfo_in_buffer(decrypted_buffer, label);
if (pass >= 0) delete_logininfo_from_buffer(decrypted_buffer, pass);
add_password_to_buffer(decrypted_buffer, label, new_string.c_str());
add_logininfo_to_buffer(decrypted_buffer, label, new_string.c_str());
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
save_buffer_to_file(encrypted_buffer);
Index* index = (Index*)decrypted_buffer.buffer;
return get_password_from_buffer(decrypted_buffer, index->count - 1);
return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1);
}
void arg_list(Buffer& decrypted_buffer)
{
Index* index = (Index*)decrypted_buffer.buffer;
Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index));
LoginInfo* pass = (LoginInfo*)(decrypted_buffer.buffer + sizeof(Index));
for (size_t i = 0; i < index->count; i++)
{
@ -70,14 +118,14 @@ void arg_list(Buffer& decrypted_buffer)
void arg_delete(Buffer& in_buffer, Buffer& out_buffer, const char* label_to_del, Cryptography& crypto)
{
printf_s("Deleting password for %s\n", label_to_del);
int pass = find_password_in_buffer(in_buffer, label_to_del);
int pass = find_logininfo_in_buffer(in_buffer, label_to_del);
if (pass < 0)
{
printf_s("Password not found\n");
return;
}
delete_password_from_buffer(in_buffer, pass);
delete_logininfo_from_buffer(in_buffer, pass);
crypto.encrypt(&in_buffer, &out_buffer);
save_buffer_to_file(out_buffer);
@ -100,7 +148,7 @@ void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass)
}
Index* index = (Index*)decrypted_buffer.buffer;
Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index));
LoginInfo* pass = (LoginInfo*)(decrypted_buffer.buffer + sizeof(Index));
for (size_t i = 0; i < index->count; i++)
{
@ -158,10 +206,10 @@ void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string&
void arg_show(Buffer& decrypted_buffer, const char* label) {
int pass = find_password_in_buffer(decrypted_buffer, label);
int pass = find_logininfo_in_buffer(decrypted_buffer, label);
if (pass < 0)
printf_s("Password not found\n");
else
printf_s("Password: %s\n", get_password_from_buffer(decrypted_buffer, pass));
printf_s("Password: %s\n", get_logininfo_pointer_from_buffer(decrypted_buffer, pass));
}

View File

@ -2,31 +2,31 @@
#include "glob.h"
#include "Buffer.h"
int find_password_in_buffer(Buffer& decrypted_buffer, const char* label)
int find_logininfo_in_buffer(Buffer& buffer, const char* label)
{
Index* index = (Index*)decrypted_buffer.buffer;
Pass* pass = (Pass*)(decrypted_buffer.buffer + sizeof(Index));
Index* index = (Index*)buffer.buffer;
LoginInfo* pass = (LoginInfo*)(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)
if (glob(label, (const char*)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)
void delete_logininfo_from_buffer(Buffer& buffer, int index_of_pass)
{
Index* in_index = (Index*)in.buffer;
Pass* in_pass = (Pass*)(in.buffer + sizeof(Index));
Index* in_index = (Index*)buffer.buffer;
LoginInfo* in_pass = (LoginInfo*)(buffer.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_of_label = strlen((char*)buffer.buffer + in_pass[index_of_pass].label + in_index->offset) + 1;
int size_of_password = strlen((char*)buffer.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);
uint8_t* start_of_pass = buffer.buffer + in_pass[index_of_pass].label + in_index->offset;
buffer.remove_fast(start_of_pass, size_pass);
for (size_t i = index_of_pass; i < in_index->count; i++)
{
@ -34,33 +34,33 @@ void delete_password_from_buffer(Buffer& in, int index_of_pass)
in_pass[i].password -= size_pass;
}
in.remove_fast((uint8_t*)&in_pass[index_of_pass], sizeof(Pass));
buffer.remove_fast((uint8_t*)&in_pass[index_of_pass], sizeof(LoginInfo));
in_index->count -= 1;
in_index->offset -= sizeof(Pass);
in_index->offset -= sizeof(LoginInfo);
}
void add_password_to_buffer(Buffer& in, const char* label, const char* password)
void add_logininfo_to_buffer(Buffer& buffer, 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);
int label_start = buffer.add_end((uint8_t*)label, strlen(label) + 1);
int password_start = buffer.add_end((uint8_t*)password, strlen(password) + 1);
Index* index = (Index*)in.buffer;
Index* index = (Index*)buffer.buffer;
label_start = label_start - index->offset;
password_start = password_start - index->offset;
Pass pass = { label_start, password_start };
LoginInfo pass = { label_start, password_start };
index->count++;
index->offset += sizeof(Pass);
index->offset += sizeof(LoginInfo);
in.add_middle((uint8_t*)&pass, sizeof(Pass), index->offset - sizeof(Pass));
buffer.add_middle((uint8_t*)&pass, sizeof(LoginInfo), index->offset - sizeof(LoginInfo));
}
const char* get_password_from_buffer(Buffer& decrypted_buffer, int label)
const char* get_logininfo_pointer_from_buffer(Buffer& buffer, int index_of_pass)
{
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;
Index* index = (Index*)buffer.buffer;
LoginInfo* pass = (LoginInfo*)(buffer.buffer + sizeof(Index));
return (const char*)buffer.buffer + pass[index_of_pass].password + index->offset;
}
void generate_password(std::string& password, int len)
@ -105,53 +105,3 @@ bool load_buffer_from_file(Buffer& buffer)
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 password of this label (or update if exists)\n");
printf_s(" -d <label> \t delete password of this label\n");
printf_s(" -i <label> \t input password for this label (or update if exists)\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;
}