Program: Password generator using text file, array of strings and call to a function



STOP PRESS! The full book is now available DIRECTLY from Amazon with lower shipping costs!

This example creates randomised passwords by joining together pairs of randomly chosen words from a file. After putting each pair of words together to make a password, the program substitutes certain letters with numeric digits or other symbols to make the passwords slightly less predictable.




The first thing that the program does is to read the words in from a text file called words.txt, storing them in a simple 1D array of string values. The program keeps count of how many words were in the file - this number is used when choosing words at random so that the program always chooses from valid positions in the array. The user is asked how many passwords they would like to make. Each password is then made by joining two randomly chosen words together. Once joined, the characters in the password are examined, substituting digits and other characters in the case of certain letters. This substitution is achieved by calling a function called mangleText, which takes the original password string as input, returning a changed string value as output.


The program needs a data file to read in, called words.txt.

Here's the data that I used:


kung
mini
maxi
mega
uber
chip
byte
plop
friz
fraz
dude
fido
mode
fish
mint
pomme
blob
flan
cake



You can copy the words and paste them into a text editor to make your own version of words.txt.



The screenshot shows the program running on an Ubuntu laptop, but it can also be compiled and executed on an Apple Mac or Raspberry Pi.

Select and copy the C++ source-code at the bottom of this post.
Paste into a text editor, such as Nano or Geany.
Then save the new file, ending in .cpp
I used genpass.cpp


To compile from the command-line:


    g++ -o genpass genpass.cpp


To run from the command-line:


    ./genpass




Here's the code:


#include <iostream>    // Displaying text on screen
#include <fstream>    // Reading from text file
#include <cstdlib>     // To use random()
#include <unistd.h>    // To see numbers using time()



using namespace std;    // Simple names for cout and endl


// --------------------


string mangleText( string textLine )
{
   // Randomly replaces letters with digits/symbols
   for ( unsigned int pos = 0; pos < textLine.length(); pos++ )
   {

       // If character is a particular letter,
       // decide whether to substitute for something different
      

       switch ( textLine.at(pos) )
       {
          case 'b':  if ( random() % 2 ) textLine.at(pos) = '8';   break;
          case 'i':  if ( random() % 2 ) textLine.at(pos) = '1';   break;
          case 'o':  if ( random() % 2 ) textLine.at(pos) = '0';   break;
          case 'e':  if ( random() % 2 ) textLine.at(pos) = '3';   break;
          case 's':  if ( random() % 2 ) textLine.at(pos) = '$';   break;

       }  // end of switch statement
   }  // end of for loop

   return textLine;
}



// --------------------


int main()
{
    ifstream wordFile;
    wordFile.open( "words.txt");
   
    const int MAX_WORDS = 100;
   
    // Words that will be amalgamated to make a random password   

    string wordArray[MAX_WORDS];
   
    string singleLine;
    int numWords = 0;  // Count how many words get read in from file
   
    // Repeatedly read in a word then store in array until end of file

    while ( wordFile.good() and numWords < MAX_WORDS )
    {
        getline( wordFile, singleLine );
        if ( singleLine.length() > 0 )
        {
            wordArray[numWords] = singleLine;
            numWords++;
        }
    }  // end of while loop
   
    // Words are now in the array, can generate passwords from them

    string firstHalf, secondHalf, password;
   
    int numWanted;
    cout << "How many passwords do you want to generate?" << endl;
    cin >> numWanted;
    cout << endl;
   
    srandom( time( 0 ) );
   
    for ( int numMade = 0; numMade < numWanted; numMade++ )
    {

        firstHalf = wordArray[ random() % numWords ];
        secondHalf = wordArray[ random() % numWords ];

        password = firstHalf + secondHalf;
        cout << password << "\t";


        // Replace letters with digits/special symbols
        password = mangleText( password );
        cout << password << endl;




    }  // end of for loop 
      
    return 0;
}



Find out more - buy the book on Amazon...






Further details in Chapter 11: Files of data