Saturday, July 28, 2018

C PROGRAM TO CREATE A STRONG PASSWORD FOR SECURITY PURPOSE


PROGRAM TO CREATE A STRONG PASSWORD FOR EMPLOYEE SECURITY:

STATEMENT:

a security committee decided to enforce the following rules when an employee creates/changes his/her password.
- The password must contain atleast one special character among # ! _ $ @
- The password must contain atleast two numbers
- The password must contain atleast one upper case alphabet and one lower case alphabet.
- The password must have a minimum length of 8.
- The password must have a maximum length of 25.

The program must accept a given password string P as input and check for these rules and output VALID or INVALID.

PROGRAM;

#include <stdio.h>

#include <regex.h>

int isValid(char *regex, char *inputpassword);

int main()
{
 
 char input[50];

    scanf("%s",input);


    char REGEX_SPECIAL[20] = "[#!_$@]+";

    char REGEX_NUMBERS[20] = ".*[0-9]+.*[0-9]+";

    char REGEX_SMALLER[20] = "[a-z]+";

    char REGEX_UPPER[20] = "[A-Z]+";

    int len = strlen(input);

    int INVALID=0;

    if(len < 8 || len > 25)
    {

        INVALID=1;
    }

    if(isInvalid(REGEX_SPECIAL,input))
    {

        INVALID=1;
    }



    if(isInvalid(REGEX_NUMBERS,input))
    {

        INVALID=1;
    }

    if(isInvalid(REGEX_SMALLER,input))
    {

        INVALID=1;
    }


    if(isInvalid(REGEX_UPPER,input))
    {

        INVALID=1;
    }


    if(INVALID)
    {

        printf("INVALID");
    }
    else
    {

        printf("VALID");
    }


}

int isInvalid(char *REGEX, char *inputpassword)
{

    regex_t reg;

    int regexcompileerror = regcomp(&reg, REGEX,REG_EXTENDED);

    if(regexcompileerror)

    {

        printf("Error in regex.");

        return 1;
    }


    if (regexec(&reg, inputpassword, 0, NULL, 0) == REG_NOMATCH)
    {

        return 1;

    }

    else
    {

        //Valid as matching the criteria

        return 0;
    }
}


Input:
m@d31nindia

Output:
INVALID

Explanation:
No alphabet in uppercase.

No comments:

Post a Comment