Nguythang

PasswordChecker.c

Feb 22nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "password_checker.h"
  5.  
  6. /* Declare conditions */
  7. char upperCases[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  8.     'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  9.     'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  10.  
  11. char lowerCases[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  12.     'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
  13.     's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  14.  
  15. char specials[] = {'`', '!', '\"', '?', '$', '?', '%', '^', '&',
  16.     '*', '(', ')', '_', '-', '+', '=', '{', '[',
  17.     '}', ']', ':', ';', '@', '\'', '~', '#', '|',
  18.     '\\', '<', '>', '.', '?'};
  19.  
  20. char numbers[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  21.  
  22. /*
  23.  * Function: CheckPasswordHealth
  24.  * ----------------------------
  25.  *   Check password health.
  26.  *   pPassword: Password string.
  27.  *   returns: Return -1: password length is smaller than 10 characters.
  28.  *            Return 0: password contains invalid characters.
  29.  *            Return 1 or return 2: password health is low.
  30.  *            Return 3: password health is medium.
  31.  *            Return 4: password health is strong.
  32.  */
  33. int CheckPasswordHealth(char *pPassword) {
  34.     int pLength = strlen(pPassword);
  35.     char invalid[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  36.         'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  37.         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  38.         'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
  39.         's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '`', '!', '\"', '?', '$', '?', '%', '^', '&',
  40.         '*', '(', ')', '_', '-', '+', '=', '{', '[',
  41.         '}', ']', ':', ';', '@', '\'', '~', '#', '|',
  42.         '\\', '<', '>', '.', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  43.     if (pLength < 10)
  44.         return -1;
  45.     else
  46.         if (pLength > 20)
  47.         return 5;
  48.     else {
  49.         int i, Count = 0;
  50.         char *checkInvalid, *checkUp, *checkLow, *checkSpecial, *checkNum;
  51.         for (i = 0; i < pLength; i++) {
  52.             checkInvalid = strchr(invalid, pPassword[i]);
  53.             if (checkInvalid == NULL)
  54.                 return 0;
  55.             break;
  56.         }
  57.         for (i = 0; i < pLength; i++) {
  58.             checkUp = strchr(upperCases, pPassword[i]);
  59.             if (checkUp != NULL) {
  60.                 Count++;
  61.                 break;
  62.             }
  63.         }
  64.         for (i = 0; i < pLength; i++) {
  65.             checkLow = strchr(lowerCases, pPassword[i]);
  66.             if (checkLow != NULL) {
  67.                 Count++;
  68.                 break;
  69.             }
  70.         }
  71.         for (i = 0; i < pLength; i++) {
  72.             checkSpecial = strchr(specials, pPassword[i]);
  73.             if (checkSpecial != NULL) {
  74.  
  75.                 break;
  76.             }
  77.             return 0;
  78.         }
  79.         for (i = 0; i < pLength; i++) {
  80.             checkNum = strchr(numbers, pPassword[i]);
  81.             if (checkNum != NULL) {
  82.                 Count++;
  83.                 break;
  84.             }
  85.         }
  86.         return Count;
  87.     }
  88. }
  89.  
  90. int main() {
  91.     printf("========== Password health checker ==========\n");
  92.     printf("This program is to check password health with 3 levels: ");
  93.     printf("low, medium and strong.\nEnter password ");
  94.     printf("(password must be no smaller than 10 and no larger than 20): ");
  95.     char *psswd;
  96.         scanf("%50[^\n]s", psswd);
  97. //    gets(psswd);
  98.  
  99.     int checker = CheckPasswordHealth(psswd);
  100.     if (checker == -1)
  101.         printf("Your password length is smaller than 10 characters.");
  102.     else if (checker == 0)
  103.         printf("Your password contain invalid characters.");
  104.     else if (checker == 1 || checker == 2)
  105.         printf("Your password health is low.");
  106.     else if (checker == 3)
  107.         printf("Your password health is medium.");
  108.     else if (checker == 4)
  109.         printf("You password health is strong.");
  110.     else if (checker == 5)
  111.         printf("Your password length is larger than 20 character.");
  112. }
Advertisement
Add Comment
Please, Sign In to add comment