Commits

Erik Grinaker  committed 7eb53b4

minor improvements to password generator

  • Participants
  • Parent commits 8bcba26

Comments (0)

Files changed (2)

 
 	* bumped version number to 0.4.6
 
+	* minor improvements to password generator
+
 2006-01-21  Erik Grinaker <erikg@codepoet.no>
 
 	* use icons instead of colors in password entries to indicate

File src/lib/util.py

 def generate_password(length, avoidambiguous = False):
 	"Generates a password"
 
-	password = []
+	def genpw(length):
+		password = []
 
-	while len(password) < length:
+		while len(password) < length:
 
-		if len(password) < int(round(length * 0.15)):
-			set = string.digits
+			if len(password) < int(round(length * 0.15)):
+				set = string.digits
 
-		elif len(password) < int(round(length * (0.15 + 0.24))):
-			set = string.ascii_lowercase
+			elif len(password) < int(round(length * (0.15 + 0.24))):
+				set = string.ascii_lowercase
 
-		elif len(password) < int(round(length * (0.15 + 0.24 + 0.24))):
-			set = string.ascii_uppercase
+			elif len(password) < int(round(length * (0.15 + 0.24 + 0.24))):
+				set = string.ascii_uppercase
+
+			else:
+				set = string.ascii_letters + string.digits
+
+			char = random.choice(set)
+
+			if avoidambiguous == True and char in "0OIl1S5qg":
+				continue
+
+			password.append(char)
+
+		random.shuffle(password)
+
+		password = "".join(password)
+
+		return password
+
+
+	# check password, and regenerate if needed
+	while 1:
+		password = genpw(length)
+
+		if length <= 6:
+			return password
+		
+		try:
+			check_password(password)
+
+		except ValueError:
+			continue
 
 		else:
-			set = string.ascii_letters + string.digits
-
-
-		char = set[int(random.random() * len(set))]
-
-		if avoidambiguous == True and char in "0OIl1S5qg":
-			continue
-
-		password.append(char)
-
-
-	random.shuffle(password)
-
-	password = "".join(password)
-
-	# check password, and regenerate if needed
-	if length < 8:
-		return password
-
-	while 1:
-		try:
-			check_password(password)
 			return password
 
-		except ValueError:
-			password = generate_password(length)
-
 
 def pad_right(string, length, padchar = " "):
 	"Right-pads a string to a given length"