forked from ChicoState/UnitTestPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword.cpp
More file actions
75 lines (66 loc) · 1.77 KB
/
Password.cpp
File metadata and controls
75 lines (66 loc) · 1.77 KB
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
#include "Password.h"
#include <string>
using std::string;
Password::Password() {
set("ChicoCA-95929");
}
/*
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){
if (phrase.empty())
return 0; // Return 0 for an empty string
int repetition = 1;
int index = 0;
while( index < phrase.length()-1 && phrase[index] == phrase[index+1] ){
repetition++;
index++;
}
return repetition;
}
bool Password::has_mixed_case(string word) {
if (word.empty()) {
return false; // Return false for an empty string
}
bool has_lower = false;
bool has_upper = false;
for (char c : word) {
if (islower(c)) {
has_lower = true;
} else if (isupper(c)) {
has_upper = true;
}
// Break the loop if both cases are found
if (has_lower && has_upper) break;
}
// Return true only if both lower and upper cases are found
return (has_lower && has_upper);
}
bool Password::set(string word) {
bool valid = true;
if ( word.length() < 8 || word.length() > 20 ){
valid = false;
}
if ( count_leading_characters(word) > 3 ) {
valid = false;
}
if ( has_mixed_case(word) == false ) {
valid = false;
}
for ( int i = 0; i < pass_history.size(); i++ ){
if ( pass_history[i] == word ){
valid = false;
}
}
if ( valid == true ) pass_history.push_back(word);
return valid;
}
bool Password::authenticate(string word) {
if ( pass_history.front() == word ){
return true;
}
return false;
}