Last active
August 29, 2015 14:04
-
-
Save refrax/27e66b11e31f390b58cd to your computer and use it in GitHub Desktop.
Python secure password generation function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def pass_gen(length=12): | |
| """Generates a random password of the length specified. Also | |
| makes sure that mimimum complexity requirements are met. | |
| This generates very strong passwords. | |
| EX: pass_gen(12) | |
| makes a 12 char password that will have at least 1 special, 1 | |
| number, and 1 lower case letter. If no length is specified, | |
| 12 is default length. | |
| """ | |
| options = 'ABCDEFGHIJKLMNPQRSTUVWXY' \ | |
| 'abcdefghijkmnopqrstuvwyz' \ | |
| '23456789' \ | |
| '$!#@%&+' | |
| s = '' | |
| try: | |
| for i in range(0, length): | |
| s += random.choice(options) | |
| # ensure that minimum password complexity is met | |
| if any(c in '$!#@%&+' for c in s) and \ | |
| any(c in '23456789' for c in s) and \ | |
| any(c in 'abcdefghijkmnopqrstuvwyz' for c in s): | |
| return s | |
| else: | |
| # if complexity not met, run again | |
| return pass_gen(length) | |
| except(ValueError, TypeError): | |
| return str(length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment