forked from ChicoState/UnitTestPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassword.cpp
101 lines (91 loc) · 2.42 KB
/
Password.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Password.h"
#include <string>
#include <cctype>
using std::string;
/*
The function receives a string counts how many times the same character
occurs at the beginning of the string, before any other characters (or the
end of the string). The function is case-sensitive so 'Z' is different than
'z' and any ASCII characters are allowed.
*/
int Password::count_leading_characters(string phrase){
int repetition = 1;
int index = 0;
while( index < phrase.length()-1 && phrase[index] == phrase[index+1] ){
repetition++;
index++;
}
return repetition;
}
/*
receives a string and returns whether it has both at least one upper-case
letter and at least one lower-case letter
*/
bool Password::has_mixed_case(string str)
{
bool upper_check = false;
bool lower_check = false;
for (int i = 0; i < str.length(); i++){
if (isupper(str[i])){
upper_check = true;
}
else if (islower(str[i])){
lower_check = true;
}
}
return upper_check && lower_check;
}
/*
constructor sets the default password to "ChicoCA-95929"
*/
Password::Password()
{
pass_history.push_back("ChicoCA-95929");
}
/*
receives a password and sets the latest in pass_history to it if and only
if it meets all criteria:
1. The password is at least 8 letters long, but no longer than 20
2. It has no more than 3 of the same leading characters
3. It has mixed case (at least one upper case and at least one lower case)
4. It was not a previous password in the history
*/
void Password::set(string str)
{
int i;
int count;
char letter;
if (str.length() >= 8 && str.length() <= 20 && has_mixed_case(str)){
for (i = 0; i < pass_history.capacity(); i++){
if (str == pass_history[i]){
exit;
}
}
letter = str[0];
count = 1;
for (i = 1; i < str.length(); i++){
if (letter == str[i]){
count++;
if (count > 3){
exit;
}
} else {
count = 1;
letter = str[i];
}
}
pass_history.push_back(str);
}
}
/*
receives a string and authenticates it against the latest password in the
pass_history, returning true for an exact match or false when it does not match
or if a password has not been set.
*/
bool Password::authenticate(string str)
{
if (str == pass_history.back()){
return true;
}
return false;
}