Added function to keyutils for generate secure passwords using secrets module

This commit is contained in:
absurdo 2023-07-21 11:43:52 +02:00
parent d31fe1e9a4
commit 24b3d40fb6

View file

@ -1,6 +1,8 @@
from hashlib import sha512, sha256
from base64 import b64encode
from os import urandom
import string
import secrets
# Functions for create random strings usando urandom
@ -40,3 +42,26 @@ def create_key(n=10):
rand_bytes=urandom(n)
return b64encode(rand_bytes).decode('utf-8')[0:-2]
def create_simple_password(n=14):
""" Based in python3 documentation for create passwords using secrets module
https://docs.python.org/3/library/secrets.html
Args:
n (int): Number of random elements of the password
"""
password=''
alphabet=string.ascii_letters+string.digits+string.punctuation
while True:
password=''.join(secrets.choice(alphabet) for i in range(n))
if (any(c.islower() for c in password) and any(c.isupper() for c in password) and sum(c.isdigit() for c in password) >= 3):
break
return password