~/blog/generador-de-contraseas-con-python

Código Completo

·3 min read·

Primero se importan las librerías necesarias:

  • Random se usa para generar números aleatorios
  • Array se usa para almacenar la contraseña generada
import random
import array

Se definen constantes y listas de posibles caracteres para la contraseña.

MAX_LEN = 6
DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
LOWERCASE_CHARACTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
    'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q',
    'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
    'z']

UPPERCASE_CHARACTERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'M', 'N', 'O', 'P', 'Q',
    'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
    'Z']

SYMBOLS = ['@', '#', '$', '%', '=', ':', '?', '.', '/', '|', '~', '>',
    '*', '(', ')', '<']

ALL_CHARACTERS = DIGITS + LOWERCASE_CHARACTERS + UPPERCASE_CHARACTERS + SYMBOLS

Se elige aleatoriamente un dígito, una letra minúscula, una mayúscula y un símbolo para garantizar que al menos estos elementos estén presentes en la contraseña.

DIGIT = random.choice(DIGITS)
LOWERCASE_CHARACTER = random.choice(LOWERCASE_CHARACTERS)
UPPERCASE_CHARACTER = random.choice(UPPERCASE_CHARACTERS)
SYMBOL = random.choice(SYMBOLS)

Se agregan caracteres aleatorios adicionales a la contraseña hasta alcanzar la longitud máxima (MAX_LEN).

for x in range(MAX_LEN - 4):
    temporal_password = temporal_password + random.choice(COMBINED_LIST)

Se crea una lista temporal de caracteres a partir de la contraseña y se realiza una mezcla aleatoria.

temporal_password_list = array.array('u', temporal_password)
random.shuffle(temporal_password_list)

Se construye la contraseña final a partir de la lista mezclada.

password = ""
for x in temporal_password_list:
    password = password + x

Finalmente, se imprime la contraseña generada.

print("Your password is: ", password)

Código Completo

import random
import array

MAX_LEN = 6

DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

LOWERCASE_CHARACTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                        'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q',
                        'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
                        'z']

UPPERCASE_CHARACTERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                        'I', 'J', 'K', 'M', 'N', 'O', 'P', 'Q',
                        'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
                        'Z']

SYMBOLS = ['@', '#', '$', '%', '=', ':', '?', '.', '/', '|', '~', '>',
           '*', '(', ')', '<']

COMBINED_LIST = LOWERCASE_CHARACTERS + UPPERCASE_CHARACTERS + SYMBOLS

rand_digit = random.choice(DIGITS)
rand_lower = random.choice(LOWERCASE_CHARACTERS)
rand_upper = random.choice(UPPERCASE_CHARACTERS)
rand_symbol = random.choice(SYMBOLS)

temporal_password = rand_digit + rand_lower + rand_upper + rand_symbol

for x in range(MAX_LEN - 4):
    temporal_password = temporal_password + random.choice(COMBINED_LIST)
    temporal_password_list = array.array('u', temporal_password)
    random.shuffle(temporal_password_list)

password = ""
for x in temporal_password_list:
    password = password + x

print("Your password is: ", password)

¿Te gustó lo que leíste?

Suscríbete para recibir los nuevos posts en tu email.