export functionality
This commit is contained in:
parent
1250fed6f9
commit
48b4402f0e
@ -12,7 +12,7 @@ public:
|
|||||||
Buffer();
|
Buffer();
|
||||||
~Buffer();
|
~Buffer();
|
||||||
bool resize(size_t new_size);
|
bool resize(size_t new_size);
|
||||||
void add(uint8_t* data, size_t data_size);
|
int add(uint8_t* data, size_t data_size);
|
||||||
|
|
||||||
// Removes data from buffer without checking if it's in the buffer
|
// Removes data from buffer without checking if it's in the buffer
|
||||||
void remove_fast(uint8_t* data, size_t data_size);
|
void remove_fast(uint8_t* data, size_t data_size);
|
||||||
|
@ -13,7 +13,7 @@ struct Pass
|
|||||||
Pass() = default;
|
Pass() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Args
|
enum class Arg
|
||||||
{
|
{
|
||||||
Get, // get password for label
|
Get, // get password for label
|
||||||
Generate, // generate password for label
|
Generate, // generate password for label
|
||||||
@ -36,4 +36,4 @@ bool load_buffer_from_file(Buffer* buffer);
|
|||||||
|
|
||||||
void print_usage();
|
void print_usage();
|
||||||
|
|
||||||
Args get_args(int argc, char** argv, char** label);
|
Arg get_args(int argc, char** argv, char** label);
|
@ -33,13 +33,14 @@ bool Buffer::resize(size_t new_size)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::add(uint8_t* data, size_t data_size)
|
int Buffer::add(uint8_t* data, size_t data_size)
|
||||||
{
|
{
|
||||||
if (taken + data_size > size)
|
if (taken + data_size > size)
|
||||||
if (!resize(size + data_size)) return;
|
if (!resize(size + data_size)) return -1;
|
||||||
|
|
||||||
memcpy_s(buffer + taken, size - taken, data, data_size);
|
memcpy_s(buffer + taken, size - taken, data, data_size);
|
||||||
taken += data_size;
|
taken += data_size;
|
||||||
|
return taken - data_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::remove_fast(uint8_t* data, size_t data_size)
|
void Buffer::remove_fast(uint8_t* data, size_t data_size)
|
||||||
|
@ -77,35 +77,35 @@ void print_usage()
|
|||||||
printf_s(" -c \t change master password\n");
|
printf_s(" -c \t change master password\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
Args get_args(int argc, char** argv, char** label) {
|
Arg get_args(int argc, char** argv, char** label) {
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
print_usage();
|
print_usage();
|
||||||
return Args::Error;
|
return Arg::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp("-h", argv[1]))
|
if (!strcmp("-h", argv[1]))
|
||||||
{
|
{
|
||||||
print_usage();
|
print_usage();
|
||||||
return Args::Error;
|
return Arg::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp("-l", argv[1])) return Args::List;
|
if (!strcmp("-l", argv[1])) return Arg::List;
|
||||||
if (!strcmp("-p", argv[1])) return Args::Print_all_p;
|
if (!strcmp("-p", argv[1])) return Arg::Print_all_p;
|
||||||
if (!strcmp("-c", argv[1])) return Args::Change;
|
if (!strcmp("-c", argv[1])) return Arg::Change;
|
||||||
|
|
||||||
if (argc < 3)
|
if (argc < 3)
|
||||||
{
|
{
|
||||||
*label = argv[1];
|
*label = argv[1];
|
||||||
return Args::Get;
|
return Arg::Get;
|
||||||
}
|
}
|
||||||
|
|
||||||
*label = argv[2];
|
*label = argv[2];
|
||||||
|
|
||||||
if (!strcmp("-g", argv[1])) return Args::Generate;
|
if (!strcmp("-g", argv[1])) return Arg::Generate;
|
||||||
if (!strcmp("-d", argv[1])) return Args::Delete;
|
if (!strcmp("-d", argv[1])) return Arg::Delete;
|
||||||
if (!strcmp("-i", argv[1])) return Args::Input;
|
if (!strcmp("-i", argv[1])) return Arg::Input;
|
||||||
if (!strcmp("-s", argv[1])) return Args::Show;
|
if (!strcmp("-s", argv[1])) return Arg::Show;
|
||||||
|
|
||||||
return Args::Error;
|
return Arg::Error;
|
||||||
}
|
}
|
@ -5,15 +5,186 @@
|
|||||||
#include "cryptography.h"
|
#include "cryptography.h"
|
||||||
#include "func.h"
|
#include "func.h"
|
||||||
|
|
||||||
|
Pass* arg_get(Buffer& decrypted_buffer, char* label)
|
||||||
|
{
|
||||||
|
Pass* pass = find_password(&decrypted_buffer, label);
|
||||||
|
if (!pass) printf_s("Password not found\n");
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pass* arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, char* label, Cryptography& crypto)
|
||||||
|
{
|
||||||
|
printf_s("Generating password for %s\n", label);
|
||||||
|
Pass* pass = find_password(&decrypted_buffer, label);
|
||||||
|
Pass new_pass = { 0 };
|
||||||
|
if (pass != nullptr) generate_password(pass->password);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
generate_password(new_pass.password);
|
||||||
|
strcpy_s(new_pass.label, MAX_STRING_SIZE, label);
|
||||||
|
int position = decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass));
|
||||||
|
if (position < 0)
|
||||||
|
{
|
||||||
|
printf_s("Error adding password\n");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
pass = (Pass*)decrypted_buffer.buffer + position;
|
||||||
|
}
|
||||||
|
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
||||||
|
save_buffer_to_file(&encrypted_buffer);
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
void arg_list(Buffer& decrypted_buffer)
|
||||||
|
{
|
||||||
|
Pass* passwords = (Pass*)decrypted_buffer.buffer;
|
||||||
|
for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++)
|
||||||
|
{
|
||||||
|
printf_s("%s\n", passwords[i].label);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void arg_delete(Buffer& decrypted_buffer, Buffer& encrypted_buffer, char* label, Cryptography& crypto)
|
||||||
|
{
|
||||||
|
printf_s("Deleting password for %s\n", label);
|
||||||
|
Pass* pass = find_password(&decrypted_buffer, label);
|
||||||
|
if (!pass)
|
||||||
|
{
|
||||||
|
printf_s("Password not found\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
decrypted_buffer.remove_fast((uint8_t*)pass, sizeof(Pass));
|
||||||
|
}
|
||||||
|
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
||||||
|
save_buffer_to_file(&encrypted_buffer);
|
||||||
|
printf_s("Password deleted\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass)
|
||||||
|
{
|
||||||
|
printf_s("Input main password for confirmation:\n");
|
||||||
|
std::string new_string = get_passwd();
|
||||||
|
if (new_string.empty())
|
||||||
|
{
|
||||||
|
printf_s("Error getting password\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (new_string != user_pass)
|
||||||
|
{
|
||||||
|
printf_s("Wrong password\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pass* passwords = (Pass*)decrypted_buffer.buffer;
|
||||||
|
for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++)
|
||||||
|
{
|
||||||
|
printf_s("%s: %s\n", passwords[i].label, passwords[i].password);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Pass* arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, char* label, Cryptography& crypto)
|
||||||
|
{
|
||||||
|
printf_s("Input password for %s:\n", label);
|
||||||
|
|
||||||
|
std::string new_string = get_passwd();
|
||||||
|
|
||||||
|
|
||||||
|
if (new_string.empty())
|
||||||
|
{
|
||||||
|
printf_s("Error getting password\n");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pass* pass = find_password(&decrypted_buffer, label);
|
||||||
|
Pass new_pass = { 0 };
|
||||||
|
|
||||||
|
|
||||||
|
if (pass != nullptr)
|
||||||
|
{
|
||||||
|
strcpy_s(pass->password, MAX_STRING_SIZE, new_string.c_str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strcpy_s(new_pass.password, MAX_STRING_SIZE, new_string.c_str());
|
||||||
|
strcpy_s(new_pass.label, MAX_STRING_SIZE, label);
|
||||||
|
int position = decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass));
|
||||||
|
if (position < 0)
|
||||||
|
{
|
||||||
|
printf_s("Error adding password\n");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
pass = (Pass*) decrypted_buffer.buffer + position;
|
||||||
|
}
|
||||||
|
|
||||||
|
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
||||||
|
save_buffer_to_file(&encrypted_buffer);
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& user_pass, Cryptography& crypto)
|
||||||
|
{
|
||||||
|
printf_s("Input main password for confirmation:");
|
||||||
|
|
||||||
|
std::string new_string = get_passwd();
|
||||||
|
if (new_string.empty())
|
||||||
|
{
|
||||||
|
printf_s("Error getting password\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_string != user_pass)
|
||||||
|
{
|
||||||
|
printf_s("Passwords don't match\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf_s("Input new password:");
|
||||||
|
new_string = get_passwd();
|
||||||
|
if (new_string.empty())
|
||||||
|
{
|
||||||
|
printf_s("Error getting password\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf_s("Input new password again:");
|
||||||
|
user_pass = get_passwd();
|
||||||
|
if (user_pass.empty())
|
||||||
|
{
|
||||||
|
printf_s("Error getting password\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_string != user_pass)
|
||||||
|
{
|
||||||
|
printf_s("Passwords don't match\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
printf_s("Password changed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void arg_show(Buffer& decrypted_buffer, char* label) {
|
||||||
|
|
||||||
|
Pass* pass = find_password(&decrypted_buffer, label);
|
||||||
|
if (!pass)
|
||||||
|
printf_s("Password not found\n");
|
||||||
|
else
|
||||||
|
printf_s("Password: %s\n", pass->password);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
char* label = nullptr;
|
char* label = nullptr;
|
||||||
Args args = get_args(argc, argv, &label);
|
Arg args = get_args(argc, argv, &label);
|
||||||
if (args == Args::Error) return 1;
|
if (args == Arg::Error) return 1;
|
||||||
|
|
||||||
Buffer encrypted_buffer;
|
Buffer encrypted_buffer;
|
||||||
if (!load_buffer_from_file(&encrypted_buffer))
|
if (!load_buffer_from_file(&encrypted_buffer))
|
||||||
if (!(args == Args::Generate)) {
|
if (!(args == Arg::Generate)) {
|
||||||
printf_s("No passwords, try generating password\n");
|
printf_s("No passwords, try generating password\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -33,178 +204,53 @@ int main(int argc, char** argv)
|
|||||||
if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1;
|
if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1;
|
||||||
|
|
||||||
Pass* pass = nullptr;
|
Pass* pass = nullptr;
|
||||||
Pass new_pass = { 0 };
|
|
||||||
Pass* passwords = nullptr;
|
|
||||||
std::string new_string;
|
|
||||||
|
|
||||||
switch (args)
|
switch (args)
|
||||||
{
|
{
|
||||||
case Args::Get:
|
case Arg::Get:
|
||||||
|
|
||||||
pass = find_password(&decrypted_buffer, label);
|
pass = arg_get(decrypted_buffer, label);
|
||||||
if (!pass)
|
if (!pass) return 1;
|
||||||
{
|
|
||||||
printf_s("Password not found\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Args::Generate:
|
|
||||||
printf_s("Generating password for %s\n", label);
|
|
||||||
pass = find_password(&decrypted_buffer, label);
|
|
||||||
|
|
||||||
if (pass != nullptr) generate_password(pass->password);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
generate_password(new_pass.password);
|
|
||||||
strcpy_s(new_pass.label, MAX_STRING_SIZE, label);
|
|
||||||
decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass));
|
|
||||||
pass = &new_pass;
|
|
||||||
}
|
|
||||||
|
|
||||||
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
|
||||||
save_buffer_to_file(&encrypted_buffer);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case Args::List:
|
case Arg::Generate:
|
||||||
|
|
||||||
passwords = (Pass*)decrypted_buffer.buffer;
|
pass = arg_generate(decrypted_buffer, encrypted_buffer, label, crypto);
|
||||||
for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++)
|
if (!pass) return 1;
|
||||||
{
|
|
||||||
printf_s("%s\n", passwords[i].label);
|
break;
|
||||||
}
|
case Arg::List:
|
||||||
|
|
||||||
|
arg_list(decrypted_buffer);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case Args::Delete:
|
case Arg::Delete:
|
||||||
printf_s("Deleting password for %s\n", label);
|
|
||||||
pass = find_password(&decrypted_buffer, label);
|
|
||||||
|
|
||||||
if (!pass)
|
arg_delete(decrypted_buffer, encrypted_buffer, label, crypto);
|
||||||
{
|
|
||||||
printf_s("Password not found\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
decrypted_buffer.remove_fast((uint8_t*)pass, sizeof(Pass));
|
|
||||||
|
|
||||||
}
|
|
||||||
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
|
||||||
|
|
||||||
save_buffer_to_file(&encrypted_buffer);
|
|
||||||
|
|
||||||
printf_s("Password deleted\n");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
break;
|
|
||||||
case Args::Print_all_p:
|
|
||||||
|
|
||||||
printf_s("Input main password for confirmation:\n");
|
|
||||||
new_string = get_passwd();
|
|
||||||
if (new_string.empty())
|
|
||||||
{
|
|
||||||
printf_s("Error getting password\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_string != user_pass)
|
|
||||||
{
|
|
||||||
printf_s("Wrong password\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
passwords = (Pass*)decrypted_buffer.buffer;
|
|
||||||
for (int i = 0; i < decrypted_buffer.taken / sizeof(Pass); i++)
|
|
||||||
{
|
|
||||||
printf_s("%s: %s\n", passwords[i].label, passwords[i].password);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
case Arg::Print_all_p:
|
||||||
|
|
||||||
case Args::Input:
|
arg_print_all_p(decrypted_buffer, user_pass);
|
||||||
printf_s("Input password for %s:\n", label);
|
|
||||||
new_string = get_passwd();
|
|
||||||
if (new_string.empty())
|
|
||||||
{
|
|
||||||
printf_s("Error getting password\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
pass = find_password(&decrypted_buffer, label);
|
|
||||||
|
|
||||||
if (pass != nullptr)
|
|
||||||
{
|
|
||||||
strcpy_s(pass->password, MAX_STRING_SIZE, new_string.c_str());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
strcpy_s(new_pass.password, MAX_STRING_SIZE, new_string.c_str());
|
|
||||||
strcpy_s(new_pass.label, MAX_STRING_SIZE, label);
|
|
||||||
decrypted_buffer.add((uint8_t*)&new_pass, sizeof(Pass));
|
|
||||||
pass = &new_pass;
|
|
||||||
}
|
|
||||||
|
|
||||||
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
|
|
||||||
save_buffer_to_file(&encrypted_buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Args::Change:
|
|
||||||
|
|
||||||
printf_s("Input main password for confirmation:");
|
|
||||||
new_string = get_passwd();
|
|
||||||
if (new_string.empty())
|
|
||||||
{
|
|
||||||
printf_s("Error getting password\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_string != user_pass)
|
|
||||||
{
|
|
||||||
printf_s("Passwords don't match\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf_s("Input new password:");
|
|
||||||
new_string = get_passwd();
|
|
||||||
if (new_string.empty())
|
|
||||||
{
|
|
||||||
printf_s("Error getting password\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf_s("Input new password again:");
|
|
||||||
user_pass = get_passwd();
|
|
||||||
if (user_pass.empty())
|
|
||||||
{
|
|
||||||
printf_s("Error getting password\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_string != user_pass)
|
|
||||||
{
|
|
||||||
printf_s("Passwords don't match\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
printf_s("Password changed\n");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
case Args::Show:
|
break;
|
||||||
|
case Arg::Input:
|
||||||
|
|
||||||
pass = find_password(&decrypted_buffer, label);
|
pass = arg_input (decrypted_buffer, encrypted_buffer, label, crypto);
|
||||||
if (!pass)
|
if (!pass) return 1;
|
||||||
{
|
|
||||||
printf_s("Password not found\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf_s("Password: %s\n", pass->password);
|
break;
|
||||||
|
case Arg::Change:
|
||||||
|
|
||||||
|
arg_change(decrypted_buffer, encrypted_buffer, user_pass, crypto);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
case Arg::Show:
|
||||||
|
|
||||||
|
arg_show(decrypted_buffer, label);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user