3

Python basic skills: Lets create an 8 characters random password generator.

 2 years ago
source link: https://dev.to/mertoenjosh/python-basic-skills-lets-create-an-8-characters-random-password-generator-3bk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is python?

According to the official python website, Python is a programming language that lets you work quickly and integrate systems more effectively.

A much more specific meaning would be: its an interpreted, object-oriented, high-level programming language with dynamic semantics.

In this article we will code a python program that generates a random 8 characters long password.

A password is a secret phrase that must be used to allow access to a computer system or service.

To ensure each password generated is strong, it must contain each of the following:

  • 2 random lower case letters
  • 2 random upper case letters
  • 2 random numeric characters
  • 2 random symbols

Breaking down the process ?

The process involves generating two characters of each type (letters, numbers and symbols), combine them in a list, shuffle them randomly and join them to a string.

List: A list is a python data type used to store multiple items in a single variable.

To begin writing the program, its important we understand the requirements.

We need to make the password as unique and random as possible. For this we need to import random.

Module: A module is a Python file that's intended to be imported into scripts

library : Python Libraries are a set of useful functions that eliminate the need for writing codes from scratch.

Let's code, shall we?

Importing the required modules.

import random
import string
Enter fullscreen modeExit fullscreen mode

String module has a pre-initialized string used as string constant, string.printable.

Create a helper function for slitting a string.

def split(word):
  return [char for char in word]
Enter fullscreen modeExit fullscreen mode

The split helper function will split string.printable into a list of all the individual charters of ascii.

all_strings = split(string.printable)
Enter fullscreen modeExit fullscreen mode

Split returns a list containing all of the ascii characters, which we store in a variable all_strings

Extract new lists for each type of ascii characters

digits = all_strings[0:10]
lowerCase = all_strings[10:36]
upperCase = all_strings[36:62]
syms = all_strings[62:94]
Enter fullscreen modeExit fullscreen mode

We use the indexing of each of the characters in the all_strings list to extract from and to the required index of each type of character.

Indexing in python starts from 0.

Create helpers functions to generate random characters of each type

# create helper functions to generate two random characters of each
def randomLower(lowerCase):
  return random.choices(lowerCase, k=2)

def randomUpper(upperCase):
  return random.choices(upperCase, k=2)

def randomDigits(digits):
  return random.choices(digits, k=2)

def randomSyms(syms):
  return random.choices(syms, k=2)
Enter fullscreen modeExit fullscreen mode

random.choices: The choices() method returns a list with the randomly selected element from the specified sequence

Concatenate the random strings in one list

temp = randomSyms(syms)+randomDigits(digits)+randomLower(lowerCase) + randomUpper(upperCase)

random.shuffle(temp)
passwd = "".join(temp)
Enter fullscreen modeExit fullscreen mode

We concatenate the generated string in one list and use the shuffle() method to shuffle the list.

The join() method is used to join items in a list by a delimiter.

Now we can print out the result

print(passwd)
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK