Thursday, 27 April 2017

Password Generator and Validation

Password Generator and Validation

Today, we will be writing code for password generator or creator, and also a validation of the given password.

We will be creating three different programs today.

1st : Password Generator


Code:

#using random function to take characters from given set, though it can generate week passwords also.

import random

s = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"

#make sure output password is 8 character long


passlen = 8
p =  "".join(random.sample(s,passlen ))
print p

2nd : Password Generator

Using inbuilt libraries




Code:
import string
import random

#creating a function to create a password using string and random function.


def pw_gen(size, 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?:\t'))))

3rd : Password validation

This forces, to put below specification in given password
  • At least 8 character long.
  • Have at least 1 digit, 1 lower, 1 capital letter.
  • Have at least 1 special character in it
Code:

import re

def validate():
    while True:
        password = raw_input("Enter a password: ")
        if len(password) < 8:
            print("Make sure your password is at lest 8 letters")
        elif re.search('[0-9]',password) is None:
            print("Make sure your password has a number in it")
        elif re.search('[A-Z]',password) is None: 
            print("Make sure your password has a capital letter in it")
        elif re.search('[@#$%^&+=]',password) is None: 
            print("Make sure your password has a Special character in it")
        else:
            print("Strong password")
            break

validate()

No comments:

Post a Comment