Skip to content

Instantly share code, notes, and snippets.

@carlos-adir
Created February 25, 2024 21:17
Show Gist options
  • Save carlos-adir/d2b518467d2e1ca263df11d3f62d3811 to your computer and use it in GitHub Desktop.
Save carlos-adir/d2b518467d2e1ca263df11d3f62d3811 to your computer and use it in GitHub Desktop.
Generate random password
import numpy as np
uppers = "QWERTYUIOPASDFGHJKLZXCVBNM"
lowers = "qwertyuiopasdfghjklzxcvbnm"
digits = list(map(str, range(10)))
specials = "!@#$%&*()+-/"
# specials = "!+-()#"
# specials = ""
chars = set(uppers)
chars |= set(lowers)
chars |= set(digits)
chars |= set(specials)
chars = tuple(chars)
def has_upper(password, quant=1):
counter = 0
for letter in set(password):
if letter in uppers:
counter += 1
return counter >= quant or counter == len(uppers)
def has_lower(password, quant=1):
counter = 0
for letter in set(password):
if letter in lowers:
counter += 1
return counter >= quant or counter == len(lowers)
def has_digit(password, quant=1):
counter = 0
for letter in set(password):
if letter in digits:
counter += 1
return counter >= quant or counter == len(digits)
def has_special(password, quant=1):
counter = 0
for letter in set(password):
if letter in specials:
counter += 1
return counter >= quant or counter == len(specials)
lenght = 20
while True:
password = []
for i in range(lenght):
index = np.random.randint(len(chars))
char = chars[index]
password.append(char)
password = "".join(password)
# print(password)
if not has_upper(password, lenght//5):
continue
if not has_lower(password, lenght//5):
continue
if not has_digit(password, lenght//5):
continue
if not has_special(password, lenght//5):
continue
break
print(password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment