Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix long non-ambiguous passwords (and insertion of numerals/symbols if forced) #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions pwgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
2015 - Luar was here <[email protected]>

"""

import string
import re

Expand All @@ -23,20 +22,35 @@
Digits = string.digits
Symbols = string.punctuation

HasLowercase = re.compile("[a-z]")
HasCaps = re.compile("[A-Z]")
HasNumerals = re.compile("[0-9]")
HasSymbols = re.compile(r"[%s]" % re.escape(Symbols))
HasAmbiguous = re.compile("[B8G6I1l|0OQDS5Z2]")


def replaceRandomChar(letter, word, pos=None):
if not pos:
"""Replace a character in the password with another.

@param letter: The character to insert into the password
@param word: The password to insert the letter into
@param pos: The position the letter shall be inserted at (may be a
position or a list of positions to choose from randomly)
"""
if isinstance(pos, list) and len(pos) > 0:
pos = choice(pos)
elif pos is None:
pos = randint(0, len(word)-1)
word = list(word)
word[pos] = letter
return "".join(word)


def findAllChars(word, pattern):
"""Return a list of positions in the string where the pattern matches."""
return [m.start() for m in re.finditer(pattern, word)]


def pwgen(pw_length=20, num_pw=1, no_numerals=False, no_capitalize=False,
capitalize=False, numerals=False, no_symbols=True, symbols=False,
allowed_symbols=None, no_ambiguous=False):
Expand Down Expand Up @@ -76,13 +90,25 @@ def pwgen(pw_length=20, num_pw=1, no_numerals=False, no_capitalize=False,
while len(passwds) < int(num_pw):
passwd = "".join(choice(letters) for x in range(pw_length))
if capitalize and not HasCaps.search(passwd):
passwd = replaceRandomChar(choice(UpperCase), passwd)
passwd = replaceRandomChar(
choice(UpperCase), passwd,
pos=findAllChars(passwd, HasLowercase)
)
if numerals and not HasNumerals.search(passwd):
passwd = replaceRandomChar(choice(Digits), passwd)
passwd = replaceRandomChar(
choice(Digits), passwd,
pos=findAllChars(passwd, HasLowercase)
)
if symbols and not HasSymbols.search(passwd):
passwd = replaceRandomChar(choice(Symbols), passwd)
if no_ambiguous and HasAmbiguous.search(passwd):
continue
passwd = replaceRandomChar(
choice(Symbols), passwd,
pos=findAllChars(passwd, HasLowercase)
)
while no_ambiguous and HasAmbiguous.search(passwd):
passwd = replaceRandomChar(
choice(letters), passwd,
pos=findAllChars(passwd, HasAmbiguous)
)
passwds.append(passwd)

if len(passwds) == 1:
Expand Down