Last active
September 20, 2017 12:05
-
-
Save LowerDeez/cdcfad1bc798c6c13eb5849e9a81a179 to your computer and use it in GitHub Desktop.
Python string (password) generator (few examples)
This file contains 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
# 1st | |
import string | |
import random | |
def pw_gen(size = 8, chars=string.ascii_letters + string.digits + string.punctuation): | |
return ''.join(random.choice(chars) for _ in range(size)) | |
print(pw_gen(int(input('How many characters in your password?')))) | |
#2nd | |
# generate a password with length "passlen" with no duplicate characters in the password | |
import random | |
s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?" | |
passlen = 8 | |
p = "".join(random.sample(s,passlen )) | |
print p | |
#3rd | |
import string | |
import random | |
def passwordGenerator(): | |
return "".join([string.printable[random.randint(0,len(string.printable)-1)] for i in range(random.randint(8,12))]) | |
print passwordGenerator() |
This file contains 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
import random | |
#setup required variables | |
lower_letters = "abcdefghijklmnopqrstuvwxyz" | |
upper_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
symbols = "!@_$%^&" | |
pass_len = int(raw_input("The required password length is: ")) | |
upper_len = int(raw_input("The required number of upper case letters is: ")) | |
symb_len = int(raw_input("The required number of symbols is: ")) | |
num_len = int(raw_input("The required number of numbers is: ")) | |
num_lower = pass_len - upper_len - symb_len - num_len | |
pw_list = [] | |
if num_lower < 0: | |
print("Sorry, the numbers you entered don't match up! Please try again ...") | |
exit() | |
#insert lowercase letters | |
i = 0 | |
while i < num_lower: | |
pw_list.append(str(''.join(random.sample(lower_letters, 1)))) | |
i += 1 | |
#insert uppercase | |
j = 0 | |
upper_inc = num_lower - 1 | |
while j < upper_len: | |
pw_list.insert(random.randint(0, upper_inc), str(''.join(random.sample(upper_letters, 1)))) | |
j += 1 | |
upper_inc += 1 | |
#insert symbols | |
k = 0 | |
symbol_inc = num_lower + upper_len - 1 | |
while k < symb_len: | |
pw_list.insert(random.randint(0, symbol_inc), str(''.join(random.sample(symbols, 1)))) | |
k += 1 | |
symbol_inc += 1 | |
#insert numbers | |
l = 0 | |
num_inc = num_lower + upper_len + symb_len - 1 | |
while l < num_len: | |
pw_list.insert(random.randint(0, num_inc), str(random.randint(0, 9))) | |
l += 1 | |
symbol_inc += 1 | |
#print password | |
print("Your generated password is: " + str(''.join(pw_list))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment