move save and load buffer to file in to buffer class
This commit is contained in:
parent
058e24d436
commit
b59c4d1d26
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
class Buffer
|
||||
{
|
||||
@ -7,6 +8,7 @@ public:
|
||||
uint8_t* buffer = nullptr;
|
||||
size_t taken = 0;
|
||||
size_t size = 0;
|
||||
std::string file_path;
|
||||
|
||||
Buffer(size_t size);
|
||||
Buffer();
|
||||
@ -26,4 +28,10 @@ public:
|
||||
|
||||
void clear() { taken = 0; }
|
||||
|
||||
bool save_to_file();
|
||||
bool save_to_file(std::string file_path);
|
||||
|
||||
bool load_from_file();
|
||||
bool load_from_file(std::string file_path);
|
||||
|
||||
};
|
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
class Buffer;
|
||||
|
||||
@ -31,7 +31,3 @@ LoginInfoPointer get_logininfo_pointer_from_buffer(Buffer& buffer, int index_of_
|
||||
|
||||
void generate_password(std::string& password, int len);
|
||||
|
||||
bool save_buffer_to_file(Buffer& buffer);
|
||||
|
||||
bool load_buffer_from_file(Buffer& buffer);
|
||||
|
||||
|
@ -78,7 +78,7 @@ std::optional<LoginInfoPointer> arg_generate(Buffer& decrypted_buffer, Buffer& e
|
||||
|
||||
add_logininfo_to_buffer(decrypted_buffer, label, password.c_str());
|
||||
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
||||
save_buffer_to_file(encrypted_buffer);
|
||||
encrypted_buffer.save_to_file();
|
||||
Index* index = (Index*)decrypted_buffer.buffer;
|
||||
return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1);
|
||||
}
|
||||
@ -99,7 +99,7 @@ std::optional<LoginInfoPointer> arg_input(Buffer& decrypted_buffer, Buffer& encr
|
||||
|
||||
add_logininfo_to_buffer(decrypted_buffer, label, new_string.c_str());
|
||||
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
||||
save_buffer_to_file(encrypted_buffer);
|
||||
encrypted_buffer.save_to_file();
|
||||
Index* index = (Index*)decrypted_buffer.buffer;
|
||||
return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1);
|
||||
}
|
||||
@ -128,7 +128,7 @@ void arg_delete(Buffer& in_buffer, Buffer& out_buffer, const char* label_to_del,
|
||||
delete_logininfo_from_buffer(in_buffer, pass);
|
||||
|
||||
crypto.encrypt(&in_buffer, &out_buffer);
|
||||
save_buffer_to_file(out_buffer);
|
||||
out_buffer.save_to_file();
|
||||
printf_s("Password deleted\n");
|
||||
}
|
||||
|
||||
@ -200,7 +200,7 @@ void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string&
|
||||
|
||||
crypto.generate_key_and_iv_from_password(new_string.c_str(), new_string.size());
|
||||
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
||||
save_buffer_to_file(encrypted_buffer);
|
||||
encrypted_buffer.save_to_file();
|
||||
printf_s("Password changed\n");
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "buffer.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
Buffer::Buffer()
|
||||
{
|
||||
@ -96,3 +97,50 @@ void Buffer::remove(uint8_t* data, size_t data_size)
|
||||
remove(index, data_size);
|
||||
}
|
||||
|
||||
bool Buffer::save_to_file()
|
||||
{
|
||||
std::ofstream file(this->file_path, std::ios::binary | std::ios::out);
|
||||
if (!file.is_open())
|
||||
{
|
||||
printf_s("Error saving file\n");
|
||||
return false;
|
||||
}
|
||||
file.write((char*)this->buffer, this->taken);
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Buffer::save_to_file(std::string file_path)
|
||||
{
|
||||
this->file_path = file_path;
|
||||
return save_to_file();
|
||||
}
|
||||
|
||||
bool Buffer::load_from_file()
|
||||
{
|
||||
std::ifstream file(this->file_path, std::ios::binary | std::ios::in);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
|
||||
size_t file_size = file.tellg();
|
||||
resize(file_size);
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read((char*)buffer, size);
|
||||
|
||||
if (file)
|
||||
taken = file_size;
|
||||
else
|
||||
taken = file.gcount();
|
||||
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Buffer::load_from_file(std::string file_path)
|
||||
{
|
||||
this->file_path = file_path;
|
||||
return load_from_file();
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <ctime>
|
||||
#include "func.h"
|
||||
#include "glob.h"
|
||||
#include "Buffer.h"
|
||||
@ -75,36 +76,3 @@ void generate_password(std::string& password, int len)
|
||||
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;
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "win.h"
|
||||
#include "buffer.h"
|
||||
#include "cryptography.h"
|
||||
@ -15,7 +14,7 @@ int main()
|
||||
if (args == Arg::Error) return 1;
|
||||
|
||||
Buffer encrypted_buffer;
|
||||
if (!load_buffer_from_file(encrypted_buffer))
|
||||
if (!encrypted_buffer.load_from_file("passwords.bin"))
|
||||
if (!(args == Arg::Generate || args == Arg::Input)) {
|
||||
printf_s("No passwords, try generating password\n");
|
||||
return 1;
|
||||
@ -86,7 +85,7 @@ int main(int argc, char** argv)
|
||||
if (args == Arg::Error) return 1;
|
||||
|
||||
Buffer encrypted_buffer;
|
||||
if (!load_buffer_from_file(encrypted_buffer))
|
||||
if (!encrypted_buffer.load_from_file("passwords.bin"))
|
||||
if (!(args == Arg::Generate || args == Arg::Input)) {
|
||||
printf_s("No passwords, try generating password\n");
|
||||
return 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user