Nguythang

Password Checker

Feb 19th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.24 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.     if (pLength < 10)
  36.         return -1;
  37.     else
  38.         if (pLength > 20)
  39.         return 5;
  40.     else {
  41.         int i, Count = 0;
  42.         char *checkUp, *checkLow, *checkSpecial, *checkNum;
  43.         for (i = 0; i < pLength; i++) {
  44.             checkUp = strchr(upperCases, pPassword[i]);
  45.             if (checkUp != NULL) {
  46.                 Count++;
  47.                 //break;
  48.             }          
  49.         }
  50.         for (i = 0; i < pLength; i++) {
  51.             checkLow = strchr(upperCases, pPassword[i]);
  52.             if (checkLow != NULL) {
  53.                 Count++;
  54.                 //break;
  55.             }
  56.         }
  57.         for (i = 0; i < pLength; i++) {
  58.             checkSpecial = strchr(upperCases, pPassword[i]);
  59.             if (checkSpecial != NULL) {
  60.                 Count++;
  61.                 //break;
  62.             }
  63.         }
  64.         for (i = 0; i < pLength; i++) {
  65.             checkNum = strchr(upperCases, pPassword[i]);
  66.             if (checkNum != NULL) {
  67.                 Count++;
  68.                 //break;
  69.             }
  70.         }
  71.         return Count;
  72.     }
  73. }
  74.  
  75. int main() {
  76.     printf("========== Password health checker ==========\n");
  77.     printf("This program is to check password health with 3 levels: ");
  78.     printf("low, medium and strong.\nEnter password ");
  79.     printf("(password must be no smaller than 10 and no larger than 20): ");
  80.     char psswd[51];
  81.     scanf("%50[^\n]s", psswd);
  82.  
  83.     int checker = CheckPasswordHealth(psswd);
  84.     if (checker == -1)
  85.         printf("Your password length is smaller than 10 characters.");
  86.     else
  87.         if (checker == 0)
  88.         printf("Your password contain invalid characters.");
  89.     else
  90.         if (checker == 1 || checker == 2)
  91.         printf("Your password health is low.");
  92.     else
  93.         if (checker == 3)
  94.         printf("Your password health is medium.");
  95.     else
  96.         if (checker == 4)
  97.         printf("You password health is strong.");
  98.     else
  99.         if (checker == 5)
  100.         printf("Your password length is larger than 20 character.");
  101. }
Advertisement
Add Comment
Please, Sign In to add comment