Skip to content

Instantly share code, notes, and snippets.

@asubramanian08
Last active July 26, 2022 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asubramanian08/e481c911a7068802e0e67cb53bde64c3 to your computer and use it in GitHub Desktop.
Save asubramanian08/e481c911a7068802e0e67cb53bde64c3 to your computer and use it in GitHub Desktop.
Store and fetch (manage) all password using one universal password (incomplete)
#include "hideInput.h"
hideInput::hideInput()
{
tcgetattr(fileno(stdin), &oflags);
nflags = oflags;
nflags.c_lflag &= ~ECHO;
nflags.c_lflag |= ECHONL;
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0)
throw "Error hiding the input";
}
hideInput::~hideInput()
{
if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0)
throw "Error reverting hidden input";
}
#include <termios.h>
#include <iostream>
//code copyied and changed from
//https://stackoverflow.com/questions/1196418/getting-a-input-in-c-without-using-getpass-3
class hideInput
{
private:
struct termios oflags, nflags;
public:
hideInput();
~hideInput();
};
NOTE: This file is used to name the gist.
#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define PASS_FILE "/Users/asubramanian/.PasswordFile.bin"
#define MAX_ATTEMPT 5
#pragma region "Verifying User"
#include "hideInput.h"
string getAPass(void)
{
//init vars
hideInput passHide;
string input;
//get input
cout << "Enter your password: ";
cin >> input;
//return
return input;
}
void handelPass(void)
{
//open file
ifstream in;
in.open(PASS_FILE);
if (in.fail())
throw "failure to open password file";
//get pass.
string password;
in >> password;
if (password.empty())
throw "unable to read password file";
//enter pass.
for (int tried = 0; getAPass() != password; tried++)
if (tried >= MAX_ATTEMPT)
throw "Password failed too many times";
}
void resetFile()
{
//handle with user
int responce;
cout << "Would you like to make a new file? (1/0): ";
cin >> responce;
if (!responce) //don't make a new file
throw "No online password system.";
//make the password file
ofstream newFile;
newFile.open(PASS_FILE);
if (newFile.fail())
throw "Encountered error opening new file";
string password = getAPass();
newFile << password;
}
#pragma endregion
int decideAction(void)
{
int action;
cout << "Would you like to ..." << endl
<< "Add a password(0)" << endl
<< "look for a password(1)" << endl
<< "Change a password(2)" << endl
<< "Change the password manager password (3)" << endl
<< "Stop this program(4)" << endl
<< "Enter your responce: ";
cin >> action;
for (int tried = 0; (action < 0) || (action > 4); tried++)
{
if (cin.fail())
throw "Standard input filed while reading action";
if (tried >= MAX_ATTEMPT)
throw "Action misentered too many times";
cout << "Invalid responce, try again: ";
cin >> action;
}
return action;
}
#pragma region "Action functions"
struct entry
{
string entryName, info;
};
struct pass
{
string title;
vector<entry> entries;
};
pass &getPass(ifstream &file)
{
}
void printPass(ofstream &file, pass &password)
{
}
void add(void)
{
//init
pass newPass;
entry currEntry;
cout << "Enter the new password's title: ";
cin >> newPass.title;
bool addInfo = true;
while (addInfo)
{
//get strings
cout << "Enter the entry name(no special chars): ";
cin >> currEntry.entryName;
if (currEntry.entryName.empty())
throw "Unable to read the entry name";
cout << "Enter the info for this entry";
cin >> currEntry.info;
if (currEntry.entryName.empty())
throw "Unable to read the entry name";
// enter and contine onward
newPass.entries.push_back(currEntry);
cout << "Do you have more entry's to make: ";
cin >> addInfo;
}
//print entry
ofstream fout;
fout.open(PASS_FILE, ofstream::app);
printPass(fout, newPass);
}
void find(void)
{
ifstream fin;
fin.open(PASS_FILE, ifstream::in);
}
void change(void)
{
}
void changeMangerPass(void)
{
}
auto quit = []() {};
void (*actions[])(void) = {add, find, change, changeMangerPass, quit};
#pragma endregion
int main(void)
{
//check password first thing
try
{
handelPass();
}
catch (const char *e)
{
cout << e << endl;
try
{
resetFile();
}
catch (const char *e)
{
cout << e << endl;
}
cout << "Failed to verify user, exiting" << endl;
return EXIT_FAILURE;
}
//decide and execute the action
try
{
actions[decideAction()];
}
catch (const char *e)
{
cout << e << endl
<< "Failed to receive action from user, exiting" << endl;
return EXIT_FAILURE;
}
//end program
cout << "Program ending" << endl;
return EXIT_SUCCESS;
}
#ifndef PASS_MAN
#define PASS_MAN
#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include "hideInput.h"
#define PASS_FILE "/Users/asubramanian/.PasswordFile.bin"
class pass_man
{
private:
static const int MAX_ATTEMPT;
static bool verified;
//verifying user
std::string getAPass();
public:
void handelPass();
void resetFile();
};
#endif //PASS_MAN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment