Welcome, guest | Sign In | My Account | Store | Cart
from os import urandom
from random import choice

char_set = {'small': 'abcdefghijklmnopqrstuvwxyz',
             'nums': '0123456789',
             'big': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
             'special': '^!\$%&/()=?{[]}+~#-_.:,;<>|\\'
            }


def generate_pass(length=21):
    """Function to generate a password"""

    password = []

    while len(password) < length:
        key = choice(char_set.keys())
        a_char = urandom(1)
        if a_char in char_set[key]:
            if check_prev_char(password, char_set[key]):
                continue
            else:
                password.append(a_char)
    return ''.join(password)


def check_prev_char(password, current_char_set):
    """Function to ensure that there are no consecutive 
    UPPERCASE/lowercase/numbers/special-characters."""

    index = len(password)
    if index == 0:
        return False
    else:
        prev_char = password[index - 1]
        if prev_char in current_char_set:
            return True
        else:
            return False

if __name__ == '__main__':
    print generate_pass()

Diff to Previous Revision

--- revision 5 2012-06-19 13:01:56
+++ revision 6 2012-06-19 13:25:03
@@ -1,9 +1,4 @@
-'''
-Created on 19 Jun 2012
-
-@author: ajay
-'''
-
+from os import urandom
 from random import choice
 
 char_set = {'small': 'abcdefghijklmnopqrstuvwxyz',
@@ -20,8 +15,8 @@
 
     while len(password) < length:
         key = choice(char_set.keys())
-        a_char = choice(char_set[key])
-        if a_char not in password:
+        a_char = urandom(1)
+        if a_char in char_set[key]:
             if check_prev_char(password, char_set[key]):
                 continue
             else:

History