Create a random password (Python)

vegaseat 0 Tallied Votes 565 Views Share

Just a simple password creator using Python code. The length of the password can be set and the characterset could be changed if you so desire.

create a random passkey/password of a given length
tested with Python version 3.4  by vegaseat (dns) on 22jul2015

import random

def createPassKeyOrdinals():
    returns a list of ordinal values of all ACII letters
    numbers and some odd characters like !#$%&
    lowers = list(range(97, 123))
    uppers = list(range(65, 91))
    numbers = list(range(48, 58))
    odds = [33, 35, 36, 37, 38]
    return lowers + uppers + numbers + odds

def getPassKey(length, ordinals):
    ''' returns a random passkey/password of given length '''
    # shuffle the ordinals in place
    # slice off a list of a given length
    myOrdinals = ordinals[0:length]
    # convert to a string
    return "".join(chr(n) for n in myOrdinals)

# testing ...
ordinals = createPassKeyOrdinals()
# set length of key
length = 10
for k in range(6):
    myKey = getPassKey(length, ordinals)

''' possible result ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.