Tutorialspoint.dev

Strong Password Suggester Program

Given a password entered by the user, check its strength and suggest some password if it is not strong.

Criteria for strong password is as follows :
A password is strong if it has :
1. At least 8 characters
2. At least one special char
3. At least one number
4. At least one upper and one lower case char.

Examples :

Input : keshav123
Output : Suggested Password
k(eshav12G3
[email protected]
kesh!Aav123
ke(shav12V3
keGshav1$23
[email protected]
keAshav12$3
[email protected]
kes$hav12N3
$kesRhav123

Input :[email protected]
Output : Your Password is Strong



Approach :
Check the input password for its strength if it fulfills all the criteria, then it is strong password else check for the absent characters in it, and return the randomly generated password to the user.

CPP

// CPP code to suggest strong password
#include <bits/stdc++.h>
  
using namespace std;
  
// adding more characters to suggest strong password
string add_more_char(string str, int need)
{
    int pos = 0;
  
    // all 26 letters
    string low_case = "abcdefghijklmnopqrstuvwxyz";
  
    for (int i = 0; i < need; i++) {
        pos = rand() % str.length();
        str.insert(pos, 1, low_case[rand() % 26]);
    }
    return str;
}
  
// make powerfull string
string suggester(int l, int u, int d, int s, string str)
{
  
    // all digits
    string num = "0123456789";
  
    // all lower case, uppercase and special characters
    string low_case = "abcdefghijklmnopqrstuvwxyz";
    string up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string spl_char = "@#$_()!";
  
    // position at which place a character
    int pos = 0;
  
    // if there is no lowercase char in input string, add it
    if (l == 0) {
        // generate random integer under string length
        pos = rand() % str.length();
  
        // generate random integer under 26 for indexing of a to z
        str.insert(pos, 1, low_case[rand() % 26]);
    }
  
    // if there is no upper case char in input string, add it
    if (u == 0) {
        pos = rand() % str.length();
        str.insert(pos, 1, up_case[rand() % 26]);
    }
  
    // if there is no digit in input string, add it
    if (d == 0) {
        pos = rand() % str.length();
        str.insert(pos, 1, num[rand() % 10]);
    }
  
    // if there is no special character in input string, add it
    if (s == 0) {
        pos = rand() % str.length();
        str.insert(pos, 1, spl_char[rand() % 7]);
    }
  
    return str;
}
  
/* make_password function :This function is used to check 
strongness and if input string is not strong, it will suggest*/
void generate_password(int n, string p)
{
    // flag for lower case, upper case, special
    // characters and need of more characters
    int l = 0, u = 0, d = 0, s = 0, need = 0;
  
    // password suggestions.
    string suggest;
  
    for (int i = 0; i < n; i++) {
        // password suggestions.
        if (p[i] >= 97 && p[i] <= 122)
            l = 1;
        else if (p[i] >= 65 && p[i] <= 90)
            u = 1;
        else if (p[i] >= 48 && p[i] <= 57)
            d = 1;
        else
            s = 1;
    }
  
    // Check if input string is strong that
    // means all flag are active.
    if ((l + u + d + s) == 4) {
        cout << "Your Password is Strong" << endl;
        return;
    }
    else
        cout << "Suggested passowrd " << endl;
  
    /*suggest 10 strong strings */
    for (int i = 0; i < 10; i++) {
        suggest = suggester(l, u, d, s, p);
        need = 8 - suggest.length();
        if (need > 0)
            suggest = add_more_char(suggest, need);
        cout << suggest << endl;
    }
}
  
// Driver code
int main()
{
    string input_string = "[email protected]";
    srand(time(NULL));
  
    // srand function set Seed for rand().
    generate_password(input_string.length(), input_string);
    return 0;
}

/div>

Java

// Java code to suggest strong password
import java.io.*;
import java.util.*;
import java.util.Random;
  
public class GFG {
  
    // adding more characters to suggest
    // strong password
    static StringBuilder add_more_char( 
                        StringBuilder str, int need)
    {
        int pos = 0;
        Random randm = new Random();
          
        // all 26 letters
        String low_case = "abcdefghijklmnopqrstuvwxyz";
  
        for (int i = 0; i < need; i++) {
            pos = randm.nextInt(1000) % str.length();
            str.setCharAt(pos,low_case.charAt(
                            randm.nextInt(1000) % 26));
        }
        return str;
    }
  
    // make powerfull String
    static StringBuilder suggester(int l, int u, int d,
                              int s, StringBuilder str)
    {
        Random randm = new Random();
          
        // all digits
        String num = "0123456789";
  
        // all lower case, uppercase and special
        // characters
        String low_case = "abcdefghijklmnopqrstuvwxyz";
        String up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String spl_char = "@#$_()!";
  
        // position at which place a character
        int pos = 0;
  
        // if there is no lowercase char in input
        // String, add it
        if (l == 0) {
              
            // generate random integer under
            // String length()
            pos = randm.nextInt(1000) % str.length();
  
            // generate random integer under 26 for
            // indexing of a to z
            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)
                                        % 26));
        }
  
        // if there is no upper case char in input 
        // String, add it
        if (u == 0) {
            pos = randm.nextInt(1000) % str.length();
            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)
                                        % 26));
        }
  
        // if there is no digit in input String, add it
        if (d == 0) {
            pos = randm.nextInt(1000) % str.length();
            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)
                                        % 10));
        }
  
        // if there is no special character in input 
        // String, add it
        if (s == 0) {
            pos = randm.nextInt(1000) % str.length();
            str.setCharAt(pos,low_case.charAt(randm.nextInt(1000)
                                        % 7));
        }
  
        return str;
    }
  
    /* make_password function :This function is used 
    to check strongness and if input String is not
    strong, it will suggest*/
    static void generate_password(int n, StringBuilder p)
    {
          
        // flag for lower case, upper case, special
        // characters and need of more characters
        int l = 0, u = 0, d = 0, s = 0, need = 0;
  
        // password suggestions.
        StringBuilder suggest;
  
        for (int i = 0; i < n; i++) {
              
            // password suggestions.
            if (p.charAt(i) >= 97 && p.charAt(i) <= 122)
                l = 1;
            else if (p.charAt(i) >= 65 && p.charAt(i) <= 90)
                u = 1;
            else if (p.charAt(i) >= 48 && p.charAt(i) <= 57)
                d = 1;
            else
                s = 1;
        }
  
        // Check if input String is strong that
        // means all flag are active.
        if ((l + u + d + s) == 4) {
            System.out.println("Your Password is Strong");
            return;
        }
        else
            System.out.println("Suggested password ");
  
        /*suggest 10 strong Strings */
        for (int i = 0; i < 10; i++) {
            suggest = suggester(l, u, d, s, p);
            need = 8 - suggest.length();
            if (need > 0)
                suggest = add_more_char(suggest, need);
            System.out.println(suggest);;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        StringBuilder input_String = new StringBuilder("[email protected]");
        generate_password(input_String.length(), input_String);
    }
}
  
// This code is contributed by Manish Shaw (manishshaw1)

C#

// C# code to suggest strong password
using System;
using System.Collections.Generic;
  
class GFG {
  
    // adding more characters to suggest
    // strong password
    static string add_more_char(string str, int need)
    {
        int pos = 0;
        Random randm = new Random();
          
        // all 26 letters
        string low_case = "abcdefghijklmnopqrstuvwxyz";
  
        for (int i = 0; i < need; i++) {
            pos = randm.Next(1,1000) % str.Length;
            str = str.Insert(pos,low_case[randm.Next(1000)
                                        % 26].ToString());
        }
        return str;
    }
  
    // make powerfull string
    static string suggester(int l, int u, int d, int s, 
                                             string str)
    {
        Random randm = new Random();
          
        // all digits
        string num = "0123456789";
  
        // all lower case, uppercase and special
        // characters
        string low_case = "abcdefghijklmnopqrstuvwxyz";
        string up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string spl_char = "@#$_()!";
  
        // position at which place a character
        int pos = 0;
  
        // if there is no lowercase char in input
        // string, add it
        if (l == 0) {
              
            // generate random integer under
            // string length
            pos = randm.Next(1,1000) % str.Length;
  
            // generate random integer under 26 for
            // indexing of a to z
            str = str.Insert(pos,low_case[randm.Next(1,
                                 1000) % 26].ToString());
        }
  
        // if there is no upper case char in input 
        // string, add it
        if (u == 0) {
            pos = randm.Next(1,1000) % str.Length;
            str = str.Insert(pos,up_case[randm.Next(
                              1,1000) % 26].ToString());
        }
  
        // if there is no digit in input string, add it
        if (d == 0) {
            pos = randm.Next(1,1000) % str.Length;
            str = str.Insert(pos,num[randm.Next(1,1000)
                                      % 10].ToString());
        }
  
        // if there is no special character in input 
        // string, add it
        if (s == 0) {
            pos = randm.Next(1,1000) % str.Length;
            str = str.Insert(pos,spl_char[randm.Next(
                              1,1000) % 7].ToString());
        }
  
        return str;
    }
  
    /* make_password function :This function is used 
    to check strongness and if input string is not
    strong, it will suggest*/
    static void generate_password(int n, string p)
    {
          
        // flag for lower case, upper case, special
        // characters and need of more characters
        int l = 0, u = 0, d = 0, s = 0, need = 0;
  
        // password suggestions.
        string suggest;
  
        for (int i = 0; i < n; i++) {
              
            // password suggestions.
            if (p[i] >= 97 && p[i] <= 122)
                l = 1;
            else if (p[i] >= 65 && p[i] <= 90)
                u = 1;
            else if (p[i] >= 48 && p[i] <= 57)
                d = 1;
            else
                s = 1;
        }
  
        // Check if input string is strong that
        // means all flag are active.
        if ((l + u + d + s) == 4) {
            Console.WriteLine("Your Password is Strong ");
            return;
        }
        else
            Console.WriteLine("Suggested password ");
  
        /*suggest 10 strong strings */
        for (int i = 0; i < 10; i++) {
            suggest = suggester(l, u, d, s, p);
            need = 8 - suggest.Length;
            if (need > 0)
                suggest = add_more_char(suggest, need);
            Console.WriteLine(suggest + " ");;
        }
    }
  
    // Driver code
    public static void Main()
    {
        string input_string = "[email protected]";
        generate_password(input_string.Length, input_string);
    }
}
  
// This code is contributed by Manish Shaw (manishshaw1)

PHP

<?php
// php code to suggest 
// strong password
  
// adding more characters to 
// suggest strong password
function add_more_char( $str, $need)
{
    $pos = 0;
  
    // all 26 letters
    $low_case = "abcdefghijklmnopqrstuvwxyz";
  
    for ($i = 0; $i < $need; $i++) 
    {
        $pos = rand() % strlen($str);
        $str[$pos]=$low_case[rand() % 26];
    }
    return $str;
}
  
// make powerfull string
function suggester($l, $u, $d, $s, $str)
{
      
    // all digits
    $num = "0123456789";
  
    // all lower case, uppercase and
    // special characters
    $low_case = "abcdefghijklmnopqrstuvwxyz";
    $up_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      
    // $spl_char1 = "@#$_()!";
    // position at which place
    // a character
    $pos = 0;
  
    // if there is no lowercase char
    // in input string, add it
    if ($l == 0) 
    {
          
        // generate random integer 
        // under string length
        $pos = rand() % strlen($str);
  
        // generate random integer under 
        // 26 for indexing of a to z
        $str[$pos]=$low_case[rand() % 26];
    }
  
    // if there is no upper case 
    // char in input string, add it
    if ($u == 0) 
    {
        $pos = rand() % strlen($str);
        $str[$pos]=$up_case[rand() % 26];
    }
  
    // if there is no digit 
    // in input string, add it
    if ($d == 0) {
        $pos = rand() % strlen($str);
        $str[$pos]=$num[rand() % 10];
    }
  
    // if there is no special character
    // in input string, add it
    if ($s == 0)
    {
        $pos = rand() % strlen($str);
        $str[$pos]=$spl_char1[rand() % 7];
    }
  
    return $str;
}
  
// Make_password function : This 
// function is used to check 
// strongness and if input string 
// is not strong, it will suggest
function generate_password($n, $p)
{
      
    // flag for lower case, upper case,
    // special characters and need
    // of more characters
    $l = 0;
    $u = 0;
    $d = 0;
    $s = 0;
    $need = 0;
  
    // password suggestions.
    $suggest;
  
    for ($i = 0; $i < $n; $i++) 
    {
        // password suggestions.
        if ($p[$i] >= 97 && $p[$i] <= 122)
            $l = 1;
        else if ($p[$i] >= 65 && $p[$i] <= 90)
            $u = 1;
        else if ($p[$i] >= 48 && $p[$i] <= 57)
            $d = 1;
        else
            $s = 1;
    }
  
    // Check if input string is strong 
    // that means all flag are active.
    if (($l + $u + $d + $s) == 4)
    {
        echo "Your Password is Strong. ";
        return;
    }
    else
        echo "Suggested passowrd";
  
    // suggest 10 strong strings 
    for ($i = 0; $i < 10; $i++) 
    {
        $suggest = suggester($l, $u, $d, $s, $p);
        $need = 8 - strlen($suggest);
        if ($need > 0)
            $suggest = add_more_char($suggest, $need);
        echo " ".$suggest;
    }
}
  
    // Driver code
    $input_string = "[email protected]";
    srand(mktime(NULL));
  
    // srand function set Seed for rand().
    generate_password(strlen($input_string), 
                      $input_string);
  
// This code is contributed by mits 
?>

Time complexity : O(n).



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter