/* * Program: Password Manager/Generator * This program will generate a password corresponding to a string that a user type in * Summer 08 - CS 31 Bridge * by Chuong Nguyen * version 1.0 * * Exercise: think of and implement a simple rule that maps a long password to one * of fixed length (say all password generated must be 8-character long). */ #include #include #include // for the "assert" function used when testing #include // for the "exit" function using namespace std; /* * generatePass will generate a password corresponding to a key, given a master password * and a list of allowed characters * -input key: a string representing a domain/site/account (ex: facebook, gmail) * -input masterPass: the master password * -input charList: a string representing allowed characters for the generated password * -output: a password corresponding to the key */ string generatePass(string key, string masterPass, string charList); /* * patchToSize will repeat a string until its length is up to a certain size * -input str: a string to patch * -input size: a desired size * -output: a new string of size 'size' that contains repetitions of str * if str's length is <= size, return str * ex: patchTosize("abc", 5) => "abcab" */ string patchToSize(string str, int size); /* * test helper functions */ void test(); // ============================== MAIN PROGRAM ============================== int main() { // uncomment these 2 lines to test our helper functions //test(); //exit(0); //at least 8 characters for the master password const string MASTER_PASS = "YourVerySecretMasterPassword"; // allowed characters for the generated password const string CHAR_LIST = "abcdefghigklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // repeating till the user type 'q' string key; do { cout << "Enter a string for a site/account to generate its password (type 'q' to quit): "; getline(cin, key); // generate a password corresponding to the key and display it cout << generatePass(key, MASTER_PASS, CHAR_LIST) << endl; } while ( key != "q" ); }// end main() // ============================== HELPER FUNCTIONS ============================== /* * generatePass will generate a password corresponding to a key, given a master password * and a list of allowed characters * -input key: a string representing a domain/site/account (ex: facebook, gmail) * -input masterPass: the master password * -input charList: a string representing allowed characters for the generated password * -output: a password corresponding to the key */ string generatePass(string key, string masterPass, string charList) { // if masterPass is shorter than the key, repeat it until its length // is the same as the key's length int masterPassLen = masterPass.length(); int keyLen = key.length(); if ( masterPassLen < keyLen ) { masterPass = patchToSize(masterPass, keyLen); } // generate password by mapping a character in the key to a new character (of charList) // simple rule: add the ASCII code of a key[i] and a masterPass[i] together // use that number to pick which character in the charList // by reducing that number to the range of 0 to charList.length()-1 string pass; int charListLen = charList.length(); for (int i = 0; i < keyLen; i++) { int index = (masterPass[i] + key[i]) % charListLen; pass += charList[index]; } return pass; }// end generatePass(key,masterPass,charList) /* * patchToSize will repeat a string until its length is up to a certain size * -input str: a string to patch * -input size: a desired size * -output: a new string of size 'size' that contains repetitions of str * if str's length is <= size, return str * ex: patchTosize("abc", 5) => "abcab" */ string patchToSize(string str, int size) { int strSize = str.length(); if ( strSize >= size ) return str; //how many times should str appears evenly in a new string 'newStr' of size 'size' int numOfAppearances = size / strSize; string newStr; for (int i = 0; i < numOfAppearances; i++) { newStr += str; } //how many remaining characters needed to fill up 'newStr' int numOfRemainingChars = size % strSize; newStr += str.substr(0, numOfRemainingChars); return newStr; }// end patchToSize(str,size) /* * test helper functions */ void test() { assert( 1 && patchToSize("abc", 2) == "abc" && patchToSize("abc", 3) == "abc" && patchToSize("abc", 4) == "abca" && patchToSize("abc", 5) == "abcab" && patchToSize("abc", 6) == "abcabc" && patchToSize("abc", 7) == "abcabca" && patchToSize("abc", 8) == "abcabcab" && patchToSize("abc", 9) == "abcabcabc" ); cerr << "=== All tests passed ===" << endl; }// end test()