99 lines
3.3 KiB
C++
99 lines
3.3 KiB
C++
#include <cstring>
|
|
|
|
#include "func.hpp"
|
|
#include "buffer.hpp"
|
|
#include "glob.hpp"
|
|
#include "cryptorand.hpp"
|
|
|
|
int find_logininfo_in_buffer(Buffer &buffer, const char *label)
|
|
{
|
|
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 *)buffer.buffer + pass[i].label + index->offset) == Glob_Result::MATCHED)
|
|
return i;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
void delete_logininfo_from_buffer(Buffer &buffer, int index_of_pass)
|
|
{
|
|
Index *in_index = (Index *)buffer.buffer;
|
|
LoginInfo *in_pass = (LoginInfo *)(buffer.buffer + sizeof(Index));
|
|
|
|
int size_of_label = strlen((char *)buffer.buffer + in_pass[index_of_pass].label + in_index->offset) + 1;
|
|
int size_of_username = strlen((char *)buffer.buffer + in_pass[index_of_pass].username + 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_username + size_of_password;
|
|
|
|
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++)
|
|
{
|
|
in_pass[i].label -= size_pass;
|
|
in_pass[i].username -= size_pass;
|
|
in_pass[i].password -= size_pass;
|
|
}
|
|
|
|
buffer.remove_fast((uint8_t *)&in_pass[index_of_pass], sizeof(LoginInfo));
|
|
in_index->count -= 1;
|
|
in_index->offset -= sizeof(LoginInfo);
|
|
}
|
|
|
|
void add_logininfo_to_buffer(Buffer &buffer, const char *label, size_t label_size, const char *username, size_t username_size, const char *password, size_t password_size)
|
|
{
|
|
uint32_t label_start = buffer.add_end((uint8_t *)label, label_size + 1);
|
|
uint32_t username_start = buffer.add_end((uint8_t *)username, username_size + 1);
|
|
uint32_t password_start = buffer.add_end((uint8_t *)password, password_size + 1);
|
|
|
|
Index *index = (Index *)buffer.buffer;
|
|
|
|
label_start = label_start - index->offset;
|
|
username_start = username_start - index->offset;
|
|
password_start = password_start - index->offset;
|
|
LoginInfo pass = {label_start, username_start, password_start};
|
|
index->count++;
|
|
index->offset += sizeof(LoginInfo);
|
|
|
|
buffer.add_middle((uint8_t *)&pass, sizeof(LoginInfo), index->offset - sizeof(LoginInfo));
|
|
}
|
|
|
|
LoginInfoPointer get_logininfo_pointer_from_buffer(Buffer &buffer, int index_of_pass)
|
|
{
|
|
Index *index = (Index *)buffer.buffer;
|
|
LoginInfo *pass = (LoginInfo *)(buffer.buffer + sizeof(Index));
|
|
LoginInfoPointer ret;
|
|
ret.label = (const char *)buffer.buffer + pass[index_of_pass].label + index->offset;
|
|
ret.username = (const char *)buffer.buffer + pass[index_of_pass].username + index->offset;
|
|
ret.password = (const char *)buffer.buffer + pass[index_of_pass].password + index->offset;
|
|
return ret;
|
|
}
|
|
|
|
bool generate_password(std::string &password, int len)
|
|
{
|
|
|
|
cryptorand pRNG;
|
|
cryptorand_result res = cryptorand_init(&pRNG);
|
|
if (res != CRYPTORAND_SUCCESS)
|
|
return false;
|
|
|
|
int buff;
|
|
|
|
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+={[}]|:;<,>.?";
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
res = cryptorand_generate(&pRNG, &buff, sizeof(buff));
|
|
if (res != CRYPTORAND_SUCCESS)
|
|
return false;
|
|
password += characters[buff % (sizeof(characters) - 1)];
|
|
}
|
|
|
|
cryptorand_uninit(&pRNG);
|
|
|
|
return true;
|
|
}
|