Program To Get A Random Strong Password

So, this is related to the "Strong Password" problem of HackerRank, which I had recently solved.

It gave me the idea to make a code that would take a password and instead of only telling the number of changes made, it would also make necessary changes to the password and display the new updated password to the user, this included the cstdlib library for generating random choices as this was a must to get different passwords every single time. I think that the code manages all the possible corner cases.

Peace!


 #include <iostream>

#include<vector>
#include<math.h>
#include<cstdlib>
#include<string.h>
using namespace std;
void minNum(string& s) {
    /*
     *
     *    Takes a password and changes so that it satisfies the
     *    basic needs of a strong password
     *
     */
 int start_choice=0;
 cout<<"Your password will be updated if it won't meet the\n conditions of 
a strong password.\nDo you wish to continue?\n0:No\t1:Yes\n";
 cin>>start_choice;
 if(start_choice==0){
     cout<<"Your password is not changed :(\n";
     return;
 }
  int ans=0,s_size=s.size();
  int capital_chk=0,num_chk=0,small_chk=0,special_chk=0;
  for(int i=0;i<s_size;i++){
      if(s[i]>=65 && s[i]<=90)capital_chk=1;
      if(s[i]>=97 && s[i]<=122)small_chk=1;
      if(s[i]>=48 && s[i]<=59)num_chk=1;
      if(!(s[i]>=65 && s[i]<=90) && !(s[i]>=97 && s[i]<=122) && 
!(s[i]>=48 && s[i]<=59))special_chk=1;
  }
  srand((unsigned)time(0));
  //  Checking if the password has a number, if not then add a number to the 
password
      if(num_chk==0){
          ans++;
          int randN=rand()%10;
          
          s.append(to_string(randN));
      }
  // No check special characters
      if(special_chk==0){
          int choice=rand()%4;
          if(choice==0)s.append("#");
          if(choice==1)s.append("$");
          if(choice==2)s.append("!");
          if(choice==3)s.append("%");
          ans++;
      }
  //Now capital letters      
      if(capital_chk==0){
          int randN=65+(rand()%26);
          string st=" ";
          char ch=(char)randN;
          st[0]=ch;
          s.append(st);
          ans++;
      }
  // Now small letters, if not present then add one to the password
      if(small_chk==0){
          ans++;
          int randN=97+(rand()%26);
          string st=" ";
          char ch=(char)randN;
          st[0]=ch;
          s.append(st);
      }
/* Now if the password is still less than 6 characters then 
 * adding more letters to it
 */   
     int current_size=s_size+ans;
     int left_chars=6-current_size;
      if(current_size<6){
          while(current_size<6){
              
              //Getting a random choice for what we should insert
              int randChoice=rand()%4;
              
              //Insert a Small Letter for 0
              if(randChoice==0){
                int randN=97+(rand()%26);
                string st=" ";
                char ch=(char)randN;
                st[0]=ch;
                s.append(st);        
              }
              //Insert a num for 1
              if(randChoice==1){
                  int randN=rand()%10;
                  s.append(to_string(randN));
              }
              //Insert a Capital Letter for 2
              if(randChoice==2){
                  int randN=65+(rand()%26);
                  string st=" ";
                  char ch=(char)randN;
                  st[0]=ch;
                  s.append(st);
              }
              //Insert a Special Character for 3
              if(randChoice==3){
                  /* 
                   * Getting a random number to select a random 
                   * special character.
                   */
                  int choice=rand()%4;
                  if(choice==0)s.append("#");
                  if(choice==1)s.append("$");
                  if(choice==2)s.append("!");
                  if(choice==3)s.append("%");
              }
            current_size++;
          }
          ans+=6-(s_size+ans);
      }
      if(ans==0){
          cout<<"Congrats! Your password was strong.\nNo changes made. :)";
return;
      }
      cout<<"Your password was changed, the number of errors were "<<ans<<".\n";
    cout<<"The new updated password is "<<s<<endl;
}

int main() {
    string s="himesh";
    minNum(s);
    return 0;
}

Comments

Popular Posts