combine input and generate

This commit is contained in:
Nikola Petrov 2023-08-28 12:28:29 +02:00
parent a76b2f5885
commit bee60daef6
3 changed files with 51 additions and 58 deletions

View File

@ -25,9 +25,7 @@ Arg get_args(int argc, char** argv, char** label);
std::optional<LoginInfoPointer> arg_get(Buffer& decrypted_buffer, const char* label);
LoginInfoPointer arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto);
std::optional<LoginInfoPointer> arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto);
std::optional<LoginInfoPointer> arg_new_password(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto, bool generate);
void arg_username(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto);

View File

@ -72,66 +72,61 @@ std::optional<LoginInfoPointer> arg_get(Buffer& decrypted_buffer, const char* la
return get_logininfo_pointer_from_buffer(decrypted_buffer, pass);
}
std::string get_user_string() {
printf_s("Input username:");
std::string ret;
std::getline(std::cin, ret);
return ret;
}
LoginInfoPointer save(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto, std::string& username, std::string& password)
std::optional<LoginInfoPointer> arg_new_password(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto, bool generate)
{
int pass = find_logininfo_in_buffer(decrypted_buffer, label);
if (pass >= 0) delete_logininfo_from_buffer(decrypted_buffer, pass);
std::string username = "";
std::string name = label;
std::string password = "";
add_logininfo_to_buffer(decrypted_buffer, label, username.c_str(), password.c_str());
if (pass >= 0) {
LoginInfoPointer lip = get_logininfo_pointer_from_buffer(decrypted_buffer, pass);
username = lip.username;
name = lip.label;
delete_logininfo_from_buffer(decrypted_buffer, pass);
}
printf_s("New password for %s: \n", name.c_str());
if (pass < 0)
{
printf_s("Input username:");
std::getline(std::cin, username);
}
if (generate)
{
int default_length = 15;
printf_s("Input password length [%d]:", default_length);
std::string input;
int length = default_length;
std::getline(std::cin, input);
if (!input.empty())
{
std::istringstream iss(input);
if (!(iss >> length)) length = default_length;
}
generate_password(password, length);
}
else
{
password = get_user_password();
if (password.empty())
{
printf_s("error getting password\n");
return {};
}
}
add_logininfo_to_buffer(decrypted_buffer, name.c_str(), username.c_str(), password.c_str());
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
encrypted_buffer.save_to_file();
Index* index = (Index*)decrypted_buffer.buffer;
return get_logininfo_pointer_from_buffer(decrypted_buffer, index->count - 1);
}
LoginInfoPointer arg_generate(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto)
{
printf_s("Generating password for %s\n", label);
std::string username = get_user_string();
int default_length = 15;
printf_s("Input password length [%d]:", default_length);
std::string input;
int length = default_length;
std::getline(std::cin, input);
if (!input.empty())
{
std::istringstream iss(input);
if (!(iss >> length)) length = default_length;
}
std::string password;
generate_password(password, length);
return save(decrypted_buffer, encrypted_buffer, label, crypto, username, password);
}
std::optional<LoginInfoPointer> arg_input(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto)
{
std::string username = get_user_string();
printf_s("Input password for %s:", label);
std::string password = get_user_password();
if (password.empty())
{
printf_s("error getting password\n");
return {};
}
return save(decrypted_buffer, encrypted_buffer, label, crypto, username, password);
}
void arg_username(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto)
{
int pass = find_logininfo_in_buffer(decrypted_buffer, label);

View File

@ -41,7 +41,7 @@ int main()
break;
case Arg::Generate:
login_info = arg_generate(decrypted_buffer, encrypted_buffer, label, crypto);
login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, crypto, true);
break;
case Arg::List:
@ -57,7 +57,7 @@ int main()
break;
case Arg::Input:
login_info = arg_input(decrypted_buffer, encrypted_buffer, label, crypto);
login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, crypto, false);
break;
case Arg::Username:
@ -127,7 +127,7 @@ int main(int argc, char** argv)
break;
case Arg::Generate:
login_info = arg_generate(decrypted_buffer, encrypted_buffer, label, crypto);
login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, crypto, true);
break;
case Arg::List:
@ -143,7 +143,7 @@ int main(int argc, char** argv)
break;
case Arg::Input:
login_info = arg_input(decrypted_buffer, encrypted_buffer, label, crypto);
login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, crypto, false);
break;
case Arg::Username: