-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.py
117 lines (93 loc) · 2.72 KB
/
password.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import click
import re
import io
from typing import Type
class Policy:
def validate(self, pw: str) -> bool:
raise NotImplementedError
@classmethod
def parse(cls, policy: str) -> 'Policy':
raise NotImplementedError
class PositionPolicy(Policy):
REGEXP = re.compile(r'(\d+)-(\d+)\s(\w)')
def __init__(
self,
char: str,
pos_a: int,
pos_b: int,
):
self.char: str = char
self.pos_a: int = pos_a
self.pos_b: int = pos_b
@classmethod
def parse(cls, policy: str) -> 'PositionPolicy':
match = cls.REGEXP.search(policy)
if match is None:
raise ValueError('not a policy string: {0}'.format(policy))
return cls(
match.group(3),
int(match.group(1)),
int(match.group(2)),
)
def has_char_at(self, pw: str, pos: int) -> bool:
if pos > len(pw):
return False
return pw[pos - 1] == self.char
def validate(self, pw: str) -> bool:
count = 0
if self.has_char_at(pw, self.pos_a):
count += 1
if self.has_char_at(pw, self.pos_b):
count += 1
return count == 1
class CountPolicy(Policy):
REGEXP = re.compile(r'(\d+)-(\d+)\s(\w)')
def __init__(
self,
char: str,
min_count: int,
max_count: int,
):
self.char: str = char
self.min_count: int = min_count
self.max_count: int = max_count
@classmethod
def parse(cls, policy: str) -> 'CountPolicy':
match = cls.REGEXP.search(policy)
if match is None:
raise ValueError('not a policy string: {0}'.format(policy))
return cls(
match.group(3),
int(match.group(1)),
int(match.group(2)),
)
def validate(self, pw: str) -> bool:
counts = {}
for char in pw:
counts[char] = counts.get(char, 0) + 1
want = counts.get(self.char, 0)
if want < self.min_count:
return False
if want > self.max_count:
return False
return True
POLICIES = {
'count': CountPolicy,
'position': PositionPolicy,
}
def get_policy(ctx, param, value) -> Type[Policy]:
return POLICIES[value]
@click.command()
@click.argument('passwords', type=click.File('rb'))
@click.argument('policy_type')
def main(passwords: io.TextIOBase, policy_type: str):
policy_cls = POLICIES[policy_type]
ok = 0
for _policy, pw in map(lambda line: str.split(str(line), ': '), passwords):
pw: str = pw.rstrip('\n')
policy = policy_cls.parse(_policy)
if policy.validate(pw):
ok += 1
print(ok)
if __name__ == '__main__':
main()