-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassgen.py
More file actions
executable file
·164 lines (135 loc) · 4.9 KB
/
passgen.py
File metadata and controls
executable file
·164 lines (135 loc) · 4.9 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
# Author: Andrew Masters
# Description: Easily create and retrieve passwords using this password generator
import argparse
import sys
import string
import secrets
import json
import os
import pyperclip
from pathlib import Path
from cryptography.fernet import Fernet
# Generates a random password of letters and numbers
def generate_password():
length = 20
alphabet = string.ascii_letters + string.digits + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(length))
return password
# Generate an encryption key in order to encrypt our password file
def get_encryption_key():
key_file = Path('my_password_encryption_key.key')
if not key_file.exists():
key = Fernet.generate_key()
with open(key_file, 'wb') as mykey:
mykey.write(key)
return key
else:
with open(key_file, 'r') as mykey:
return mykey.read()
# encrypt the password file and then delete it
def encrypt_file(file):
f = Fernet(get_encryption_key())
with open(file, 'rb') as original_file:
original = original_file.read()
encrypted = f.encrypt(original)
with open('enc_passwords.txt', 'wb') as encrypted_file:
encrypted_file.write(encrypted)
os.remove(file)
# Decrypt file and return dictionary of names and passwords
def decrypt_file():
f = Fernet(get_encryption_key())
with open('enc_passwords.txt', 'rb') as encrypted_file:
encrypted = encrypted_file.read()
decrypted = f.decrypt(encrypted)
pw_dict = json.loads(decrypted)
return pw_dict
# Fetch the password for the name given
def fetch_password_for_name(name):
encryption_file = Path('enc_passwords.txt')
if not encryption_file.exists():
return ''
pw_dict = decrypt_file()
pw = pw_dict.get(name, '')
return pw
# Requests a name for the password that matches to it
def add_password_with_name(name):
enc_passwords = Path('enc_passwords.txt')
if enc_passwords.exists():
pw_dict = decrypt_file()
pw_dict[name] = generate_password()
else:
pw_dict = {name : generate_password()}
print(f'New password generated for {name}')
new_file = Path('temp_pws.txt')
with open(new_file, 'w') as raw_file:
raw_file.write(json.dumps(pw_dict))
encrypt_file(new_file)
# Main program starter for this file
def passgen(name):
#name = input('pw name: ')
pw = fetch_password_for_name(name)
if pw == '':
print(f'Password for {name} does not exist, generating new password!')
add_password_with_name(name)
pw = fetch_password_for_name(name)
else:
print("Password found")
print('Password copied to clipboard.')
pyperclip.copy(pw)
# Remove a password from the encrypted file and
def passgen_remove_name(name):
enc_passwords = Path('enc_passwords.txt')
if enc_passwords.exists():
pw_dict = decrypt_file()
else:
print("There are no passwords to delete.")
return
if pw_dict.get(name, '') == '':
print(f'Password for {name} could not be deleted because it does not exist.')
return
del pw_dict[name]
new_file = Path('temp_pws.txt')
with open(new_file, 'w') as raw_file:
raw_file.write(json.dumps(pw_dict))
encrypt_file(new_file)
print(f'Password for {name} has been deleted.')
# Fetch all names from the encrypted file
def fetch_names():
encryption_file = Path('enc_passwords.txt')
if not encryption_file.exists():
print('Could not retrieve encrypted password file.')
return
pw_dict = decrypt_file()
keys = pw_dict.keys()
for key in keys:
print(key)
def fetch_names_with_passwords():
encryption_file = Path('enc_passwords.txt')
if not encryption_file.exists():
print('Could not retrieve encrypted password file.')
return
pw_dict = decrypt_file()
keys = pw_dict.keys()
for key, value in pw_dict.items():
print(key, ' -> ', value)
def main():
parser=argparse.ArgumentParser(description='Parse out the name.')
parser.add_argument("-fetch", help="fetch the password associated with the name", type=str)
parser.add_argument("-show_names", nargs='?', const=True, help="view all names", type=bool)
parser.add_argument("-show_passwords", nargs='?', const=True, help="show all passwords with their associated name", type=bool)
parser.add_argument("-remove_password", help="remove the password of the given name", type=str)
args = parser.parse_args()
# Ensure that the name variable is not null
if args.fetch is not None:
passgen(args.fetch)
if args.remove_password is not None:
passgen_remove_name(args.remove_password)
# Fetch all the password names if requested
if args.show_names:
fetch_names()
# Fetch all names with their passwords
if args.show_passwords:
fetch_names_with_passwords()
if __name__=="__main__":
main()