Skip to content

Commit

Permalink
Improve password generation
Browse files Browse the repository at this point in the history
Passwords were generated by randomly choosing items in a single group formed by
the required characters, digits and symbols. The problem with that way
of choosing items is that it doesn't consider the types of the items,
thus getting more chances of choosing a letter than a number, given that
the letters group is bigger than the numbers group.
Now, I create a list with each required type of element (lower case
chars, upper case chars, digits and symbols) and randomly pick one. Then
I pick an item of that group. This way, each type of element has the
same probability of being picked.
  • Loading branch information
HacKanCuBa committed Jul 19, 2018
1 parent 0168567 commit c30ef0e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions passphrase/passphrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,26 @@ def _read_words_from_diceware(inputfile: str) -> list:
word.split()[1] for word in open(inputfile, mode='rt')
]

def _get_password_characters(self) -> str:
def _get_password_characters(self, cathegorized=False) -> str:
from string import (
digits,
ascii_lowercase,
ascii_uppercase,
punctuation
)

characters = ''
group = []

if self.password_use_lowercase:
characters += ascii_lowercase
group.append(ascii_lowercase)
if self.password_use_uppercase:
characters += ascii_uppercase
group.append(ascii_uppercase)
if self.password_use_digits:
characters += digits
group.append(digits)
if self.password_use_punctuation:
characters += punctuation
group.append(punctuation)

return characters
return group if cathegorized else ''.join(group)

def __init__(
self,
Expand Down Expand Up @@ -472,16 +472,18 @@ def generate_password(self) -> list:
"""Generates a list of random characters."""

password = []
characters = self._get_password_characters()
characterset = self._get_password_characters(cathegorized=True)
if (
self.passwordlen is None
or not characters
or not characterset
):
raise ValueError("Can't generate password: character set is "
"empty or passwordlen isn't set")

for _ in range(0, self.passwordlen):
password.append(randchoice(characters))
# The first choice is done to choose the set
# The second chooses an item in that set
password.append(randchoice(randchoice(characterset)))

self.last_result = password
return password
Expand Down

0 comments on commit c30ef0e

Please sign in to comment.