Erik Grinaker avatar Erik Grinaker committed 2608864

rewrote the password generator

Comments (0)

Files changed (3)

 
 	* bumped version number to 0.4.6
 
-	* minor improvements to password generator
+	* rewrote the password generator
 
 2006-01-21  Erik Grinaker <erikg@codepoet.no>
 
 - add import/export of gnukeyring files
 - add import/export of web confidential files
 - add import/export from browser (firefox, mozilla, epiphany, galeon)
+- improve password checking and password generation
 
 0.5.x:
 - ensure complete UTF-8 support
 	"Runs a command as a child, returns its process ID"
 
 	items = shlex.split(command.encode("iso-8859-1"), 0)
+
 	return os.spawnvp(os.P_NOWAIT, items[0], items)
 
 
 def generate_password(length, avoidambiguous = False):
 	"Generates a password"
 
+	# set up character sets
+	d	= string.digits
+	lc	= string.ascii_lowercase
+	uc	= string.ascii_uppercase
+
+	if avoidambiguous == True:
+		d	= d.translate(string.maketrans("", ""), "015")
+		lc	= lc.translate(string.maketrans("", ""), "lqg")
+		uc	= uc.translate(string.maketrans("", ""), "IOS")
+
+	charsets = (
+		( d,	0.15 ),
+		( uc,	0.24 ),
+		( lc,	0.24 ),
+	)
+
+	
+	# function for generating password
 	def genpw(length):
 		password = []
 
+		for set, share in charsets:
+			password.extend([ random.choice(set) for i in range(int(round(length * share))) ])
+
 		while len(password) < length:
-
-			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 + 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)
+			password.append(random.choice(d + lc + uc))
 
 		random.shuffle(password)
 
-		password = "".join(password)
-
-		return password
+		return "".join(password)
 
 
 	# check password, and regenerate if needed
 	while 1:
-		password = genpw(length)
+		try:
+			password = genpw(length)
 
-		if length <= 6:
+			if length <= 6:
+				return password
+
+			check_password(password)
+
 			return password
-		
-		try:
-			check_password(password)
 
 		except ValueError:
 			continue
 
-		else:
-			return password
-
 
 def pad_right(string, length, padchar = " "):
 	"Right-pads a string to a given length"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.