Files
2023-03-25 03:30:18 +00:00

136 lines
4.0 KiB
C

/*
COS135 HW7
Nicholas Pease
Password Validation
*/
#include <stdio.h>
#include <ctype.h>
int validateSpecialCharacter(char password[]) {
int flag = 0;int i = 0;
while (flag == 0 && password[i] != '\n') {
switch(password[i]) {
case '!':
flag = 1;
break;
case '@':
flag = 1;
break;
case '#':
flag = 1;
break;
case '$':
flag = 1;
break;
}
i++;
}
return flag;
}
int validateHasDigit(char password[]) {
int flag = 0;int i = 0;
while (flag == 0 && password[i] != '\n') {
if (password[i] > '0' && password[i] < '9')
flag = 1;
i++;
}
return flag;
}
int validateLength(char password[]) {
// counts characters until '\n' is found
int count = 0;int i = 0;
while(password[i] != '\n') {
count++;
i++;
}
return count >= 8? 1: 0;
}
int validateUpperLowerCase(char password[]) {
int upper = 0; int lower = 0;int i = 0;
while(password[i] != '\n') {
if (password[i] >= 'A' && password[i] <= 'Z')
upper = 1;
if (password[i] >= 'a' && password[i] <= 'z')
lower = 1;
i++;
}
// both must be true to pass flag
return upper+lower==2? 1:0;
}
// extension
// password is directly modified
void encryptPassword(char password[]) {
int i = 0;
while (password[i] != '\n') {
if (isalnum(password[i])) {
password[i] = password[i] + 1;
} else {
switch (password[i]) {
case '!':
password[i] = password[i] - 1;
break;
case '@':
password[i] = password[i] - 1;
break;
case '#':
password[i] = password[i] - 1;
break;
case '$':
password[i] = password[i] - 1;
break;
}
}
i++;
}
// remove newline and null terminate
password[i] = 0x00;
}
int main() {
//input
printf("Please enter your prefered password: ");
char password[20];
fgets(password,20,stdin);
// set state of all flags
int validLength = validateLength(password);
int includesUpperLower = validateUpperLowerCase(password);
int hasNumber = validateHasDigit(password);
int hasSpecialCharacter = validateSpecialCharacter(password);
// contains number of "tripped" flags
int inverseCombinedFlag = !validLength + !includesUpperLower + !hasNumber + !hasSpecialCharacter;
// all functions must return 1 (and add to 4), for it to be considered a valid password
printf("%s\n",(validLength + includesUpperLower + hasNumber + hasSpecialCharacter == 4)? "Valid Password": "Invalid Password");
// if invalid, showcase everything wrong with it. Otherwise encrypt
if (validLength + includesUpperLower + hasNumber + hasSpecialCharacter != 4) {
for (int i = 1; i < inverseCombinedFlag + 1; i++) {
if (!validLength) {
printf("Reason #%d your password should contain at least eight characters\n",i);
validLength = 1;
} else if (!includesUpperLower) {
printf("Reason #%d your password should contain at least one lowercase character and one uppercase letter\n",i);
includesUpperLower = 1;
} else if (!hasSpecialCharacter) {
printf("Reason #%d your password should contain at least one of the four special characters “! @ # $”\n",i);
hasSpecialCharacter = 1;
} else if (!hasNumber) {
printf("Reason #%d your password should contain at least one digit\n",i);
hasNumber = 1;
}
}
} else {
encryptPassword(password);
printf("Encrypted Password: %s\n",password);
}
return 0;
}