-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_manager.py
72 lines (65 loc) · 2.39 KB
/
password_manager.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
# == INSTRUCTIONS ==
#
# Purpose: Manage a user's (valid) passwords
#
# Methods:
# 1. Name: __init__
# Arguments: none
# 2. Name: add
# Purpose: add a password for a service IF it is valid, otherwise do nothing
# Arguments: one string representing a service name,
# one string representing a password
# Returns: None
# 3. Name: get_for_service
# Arguments: one string representing a service name
# Returns: the password for the given service, or None if none exists
# 4. Name: list_services
# Arguments: none
# Returns: a list of all the services for which the user has a password
#
# A reminder of the validity rules:
# 1. A password must be at least 8 characters long
# 2. A password must contain at least one of each of the following:
# - It must be longer than 7 characters (8 is fine)
# - It must contain at least one of the following special characters:
# `!`, `@`, `$`, `%` or `&`
#
# We recommend that you store the passwords in a dictionary, where the keys are
# the service names and the values are the passwords.
#
# Example usage:
# > password_manager = PasswordManager()
# > password_manager.add('gmail', '12ab5!678') # Valid password
# > password_manager.add('facebook', '$abc1234') # Valid password
# > password_manager.add('twitter', '12345678') # Invalid password, so ignored
# > password_manager.get_for_service('facebook')
# '$abc1234'
# > password_manager.get_for_service('not_real')
# None
# > password_manager.get_for_service('twitter')
# None
# > password_manager.list_services()
# ['gmail', 'facebook']
#
# == YOUR CODE ==
class PasswordManager():
def __init__(self):
self.password_store = {}
def add(self, service_name, password):
self.service_name = service_name
self.password = password
specials = ["!", "@", "$", "%", "&"]
if len(password) > 7:
if any(c in specials for c in password):
self.password_store[service_name] = password
else:
return False
else:
return False
def get_for_service(self, service_name):
if service_name in self.password_store:
return self.password_store.get(service_name, self.password)
else:
return None
def list_services(self):
return self.password_store.keys()