You are not logged in.

#1 2007-07-13 03:11:53

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

python password module

I got bored and wrote a password module for python. Enjoy.

#!/usr/bin/python
#
# pypasswd.py
#
# Written By: Tyler Gates

import os, sys, crypt, string, getpass
from random import choice

class password:
        MD5saltprefix                   = "$1$"
        DESsaltprefix                   = ""
        valid_salt_set                  = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/."
        valid_password_set              = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[]^_`{|}~\ "

        def gen_random(self, set, length):
                """ generate random string length long from set """
                random_string = ""
                for i in range(length):
                        random_string = random_string + choice(set)
                return random_string

        def gen_MD5salt(self):
                """ generate a random MD5 salt eight long """
                return self.MD5saltprefix + self.gen_random(self.valid_salt_set, 8)

        def gen_DESsalt(self):
                """ generate a random DES salt two long """
                return self.DESsaltprefix + self.gen_random(self.valid_salt_set, 2)

        def gen_password(self, length):
                """ generate a random password string length long """
                return self.gen_random(self.valid_password_set, length)

        def cleartext(self):
                """ prompt for a password and return the string in clear text """
                return getpass.getpass()

        def encrypt(self, salt):
                """ prompt for a password and return the string encrypted using salt """
                _cleartext = self.cleartext()
                return crypt.crypt(_cleartext, salt)

if __name__ == "__main__":
        # this will print a UNIX/Linux md5 encrypted password to screen
        md5salt                                 = password().gen_MD5salt()
        encrypted_md5password   = password().encrypt(md5salt)
        print encrypted_md5password

Offline

Board footer

Powered by FluxBB