Commit 0590504d authored by Benjamin REED's avatar Benjamin REED

added dictionary

parent 373857fb
This diff is collapsed.
......@@ -14,6 +14,8 @@ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import rsa
from rsa import RsaKey
DICTIONARY_PATH = "French.dic"
DEFAULT_DATABASE_PATH = "db.txt"
USAGES = {
"new":
......@@ -333,11 +335,25 @@ class State:
"""
Generate a password given a 'set' object of characters to choose from.
"""
def generate_password(length, charset):
s = []
for i in range(0, length):
s.append(random.choice(list(charset)))
return ''.join(s)
def generate_password(length, charset, words: bool = False):
if words:
s = []
dic = open(DICTIONARY_PATH)
lines = dic.read().split('\n') # 2 MEGABYTES by the way
for line in lines:
word = line.split('/')[0]
if len(word) >= length: continue
if len(s) + len(word) >= length:
dic.close()
return ''.join(s) + generate_password(length - len(s), charset, False)
s.extend(list(word))
dic.close()
print("Catastrophic error generating password.")
else:
s = []
for i in range(0, length):
s.append(random.choice(list(charset)))
return ''.join(s)
def handle_args(db: str, password: str):
state = State(db, password)
......@@ -627,18 +643,28 @@ def main():
continue
chars = set()
words = False
if charset == "all":
chars = LOWERCASE_ALPHABET | UPPERCASE_ALPHABET | NUMBERS | SPECIAL
else:
s = charset.split("-")
for c in list(s[0]):
chars |= CHARSET_MAP[c]
if c == 'w' or c == 'm': # words/mots
words = True
chars |= LOWERCASE_ALPHABET | UPPERCASE_ALPHABET | NUMBERS | SPECIAL
elif c not in CHARSET_MAP:
print(f"Unrecognized charset option '{c}', expected 'l', 'u', 'n', 's', or 'w'")
else:
chars |= CHARSET_MAP[c]
if len(s) > 1:
for c in s[1]: # list of chars to exclude
chars ^= set(c)
if chars == set():
print("Error: unrecognizable charset provided")
continue
password = generate_password(length, chars)
password = generate_password(length, chars, words)
print(password)
state.add_password(label, password.encode(), mode, key)
elif command == "list" or command == "ls":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment