Advertisement
BenTibnam

Really Bad Password Manager - RBPM C++

Oct 10th, 2021 (edited)
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4.  
  5. /*
  6.     Random Password Generator located @ https://pastebin.com/fX0D26Je
  7. */
  8.  
  9. // Program allows for 128 services to be stored
  10. const int MAX_SERVICES = 128;
  11. const std::string SERVICE_FILE_NAME = "service_list.txt";
  12.  
  13. bool correctCommandSyntax(int argc);
  14. std::string getServicePassword(std::string serviceName);
  15. void addService(std::string serviceName, std::string servicePassword);
  16. void removeService(std::string serviceName);
  17. void runSetup();
  18. bool setupRequired();
  19. std::string getFileLine(int line, std::string fileName);
  20. std::string getFileLine(int line);
  21. void getServiceData();
  22. void listServices();
  23. int getIndexOfCharacterInString(std::string str, char c);
  24.  
  25. int main(int argc, char** argv)
  26. {
  27.         // Check to insure that issued command is in the right syntax
  28.         if (!correctCommandSyntax(argc))
  29.         {
  30.                 std::cout << "Bad syntax.\nUse pass <service_name> or pass add" << std::endl;
  31.                 return 1;
  32.         }
  33.  
  34.         // Checking if we need to run program setup before move on
  35.         if (setupRequired())
  36.         {
  37.                 std::cout << "Creating required files." << std::endl;
  38.                 runSetup();
  39.         }
  40.  
  41.         std::string issuedCommand = argv[1];
  42.  
  43.         // Start function for adding service to list
  44.         if (issuedCommand == "add")
  45.         {
  46.                 getServiceData();
  47.         }
  48.         else if (issuedCommand == "remove")
  49.         {
  50.                 std::string serviceName;
  51.                 std::cout << "What service do you want to remove? ";
  52.                 std::cin >> serviceName;
  53.                 removeService(serviceName);
  54.         }
  55.         else if(issuedCommand == "generatep")
  56.         {
  57.                 system("python3 rand.py");
  58.         }
  59.         else if(issuedCommand == "list")
  60.         {
  61.                 listServices();
  62.         }
  63.  
  64.         else
  65.         {
  66.                 std::cout << getServicePassword(issuedCommand) << std::endl;
  67.         }
  68.  
  69.  
  70.  
  71.         return 0;
  72. }
  73.  
  74. bool correctCommandSyntax(int argc)
  75. {
  76.         return argc == 2;
  77. }
  78.  
  79. std::string getServicePassword(std::string serviceName)
  80. {
  81.         // The delimiter marks where service name ends and password begins
  82.         std::string password = "";
  83.         char delimiter = ':';
  84.  
  85.         for (int i = 0; i < MAX_SERVICES; i++)
  86.         {
  87.                 // rest password value after each time there is incorrect data in the variable
  88.                 password = "";
  89.                 std::string currentLine = getFileLine(i);
  90.  
  91.                 // If the current line is nothing that means that we don't need to look through the array anymore, because nothing else will be filled
  92.                 if (currentLine == "")
  93.                 {
  94.                         break;
  95.                 }
  96.  
  97.                 // splitting the password and service into two different variables
  98.                 std::string service = currentLine.substr(0, currentLine.find(delimiter));
  99.                 password = currentLine.substr(currentLine.find(delimiter) + 1);
  100.  
  101.                 // The service has been found
  102.                 if(service == serviceName)
  103.                 {
  104.                         break;
  105.                 }
  106.         }
  107.  
  108.         return password;
  109. }
  110.  
  111. void addService(std::string serviceName, std::string servicePassword)
  112. {
  113.         // Checking for banned service name
  114.         std::string bannedServiceName = "add";
  115.         if (serviceName == bannedServiceName)
  116.         {
  117.                 std::cout << "Service name cannot be " << bannedServiceName << std::endl;
  118.                 return;
  119.         }
  120.  
  121.         // Appending service name and password to file
  122.         std::ofstream outputFile;
  123.         outputFile.open(SERVICE_FILE_NAME, std::ios::app);
  124.         outputFile << serviceName << ':' << servicePassword << '\n';
  125.         outputFile.close();
  126. }
  127.  
  128. void removeService(std::string serviceName)
  129. {
  130.         int lineToDelete = -1;
  131.         char delimiter = ':';
  132.  
  133.         // finding out what line needs to be deleted
  134.         for (int i = 0; i < MAX_SERVICES; i++)
  135.         {
  136.                 // if the current line is empty, that means there are no more services to go over
  137.                 if (getFileLine(i) == "")
  138.                 {
  139.                         break;
  140.                 }
  141.                 else
  142.                 {
  143.                         // finding service of current line and check if it's the name of looked for service
  144.                         std::string currentLine = getFileLine(i);
  145.                         std::string service = currentLine.substr(0, currentLine.find(delimiter));
  146.  
  147.                         if (service == serviceName)
  148.                         {
  149.                                 lineToDelete = i;
  150.                                 break;
  151.                         }
  152.                         else
  153.                         {
  154.                                 continue;
  155.                         }
  156.                 }
  157.         }
  158.  
  159.         // returning if no service was found on the list
  160.         if (lineToDelete == -1)
  161.         {
  162.                 std::cout << "Service not found" << std::endl;
  163.                 return;
  164.         }
  165.  
  166.         // writing file contents into a buffer file
  167.         std::string bufferFileName = "service_buffer.txt";
  168.         std::ofstream buffer;
  169.         buffer.open(bufferFileName, std::ios::app);
  170.  
  171.         // copying all lines, but the one being deleted
  172.         for (int i = 0; i < MAX_SERVICES; i++)
  173.         {
  174.                 // skip if we are on the line to delete
  175.                 if (i == lineToDelete)
  176.                 {
  177.                         continue;
  178.                 }
  179.                 else
  180.                 {
  181.                         buffer << getFileLine(i);
  182.                 }
  183.         }
  184.  
  185.         buffer.close();
  186.  
  187.         // clearing old file
  188.         std::ofstream output;
  189.         output.open(SERVICE_FILE_NAME);
  190.         output << "";
  191.         output.close();
  192.  
  193.         // writing new file
  194.         output.open(SERVICE_FILE_NAME, std::ios::app);
  195.         for (int i = 0; i < MAX_SERVICES; i++)
  196.         {
  197.                 output << getFileLine(i, bufferFileName);
  198.         }
  199.         output.close();
  200.         system("rm service_buffer.txt");
  201.  
  202.  
  203.         return;
  204.  
  205. }
  206.  
  207. void runSetup()
  208. {
  209.         std::ofstream outputFile;
  210.         outputFile.open(SERVICE_FILE_NAME);
  211.         outputFile.close();
  212.  
  213. }
  214.  
  215. bool setupRequired()
  216. {
  217.         std::ifstream inputFile;
  218.         inputFile.open(SERVICE_FILE_NAME);
  219.  
  220.         return !inputFile.is_open();
  221. }
  222.  
  223. std::string getFileLine(int line, std::string fileName)
  224. {
  225.         // Opening service file to search for password
  226.         std::ifstream in;
  227.         in.open(fileName);
  228.  
  229.         // Placeholder holds the final value while buffer helps continue the iteration through the loop
  230.         std::string placeholder = "";
  231.         std::string buffer = "";
  232.         for (int i = 0; i < MAX_SERVICES; i++)
  233.         {
  234.                 // When we reach the requested line place that value into place holder and leave loop
  235.                 if (i == line)
  236.                 {
  237.                         in >> placeholder;
  238.                         break;
  239.                 }
  240.  
  241.                 // put current lines input into buffer so iteration through the file can continue
  242.                 in >> buffer;
  243.         }
  244.  
  245.         // The value in placeholder is the value of the line
  246.         if (placeholder != "")
  247.         {
  248.                 return placeholder + '\n';
  249.         }
  250.  
  251.         // return the empty line so we don't have a ton of whitespace filling the window
  252.         return placeholder;
  253. }
  254.  
  255. std::string getFileLine(int line)
  256. {
  257.         return getFileLine(line, SERVICE_FILE_NAME);
  258. }
  259.  
  260. void getServiceData()
  261. {
  262.         std::cout << "Add service to list" << std::endl;
  263.         std::string serviceName;
  264.         std::string servicePass;
  265.  
  266.         std::cout << "Name: ";
  267.         std::cin >> serviceName;
  268.         std::cout << "Pass: ";
  269.         std::cin >> servicePass;
  270.         addService(serviceName, servicePass);
  271. }
  272.  
  273. int getIndexOfCharacterInString(std::string str, char c)
  274. {
  275.         int size = str.size();
  276.  
  277.         // searching through string and returning the index if found
  278.         for (int i = 0; i < size; i++){
  279.                 if (str[i] == c)
  280.                 {
  281.                         return i;
  282.                 }
  283.         }
  284.  
  285.         // string wasn't found
  286.         return -1;
  287. }
  288.  
  289. void listServices()
  290. {
  291.         char delim = ':';
  292.  
  293.         for (int i = 0; i < MAX_SERVICES; i++)
  294.         {
  295.                 std::string currentLine = getFileLine(i);
  296.  
  297.                 // quit operation if we have exhausted the list
  298.                 if (currentLine == "")
  299.                 {
  300.                         return;
  301.                 }
  302.                 else
  303.                 {
  304.                         int indexOfDelimeter = getIndexOfCharacterInString(currentLine, delim);
  305.                         std::string serviceName = currentLine.substr(0, indexOfDelimeter);
  306.                         std::cout << serviceName << std::endl;
  307.                 }
  308.         }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement