Create a random password (Python)

vegaseat 0 Tallied Votes 570 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.

''' pass_key_create101.py
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
    random.shuffle(ordinals)
    # 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)
    print(myKey)

''' possible result ...
onQv$53j9P
Avn$o60hjb
3EdPJXniz5
6JE1RVeTpv
4ubgwXQDc#
ZBTNDm4Eng
'''