<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python Blog - All about python &#187; md5</title>
	<atom:link href="http://www.python-blog.com/tag/md5/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.python-blog.com</link>
	<description>and technologies around</description>
	<lastBuildDate>Tue, 27 Jul 2010 19:58:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Python random password generator</title>
		<link>http://www.python-blog.com/2009/06/23/python-random-password-generator/</link>
		<comments>http://www.python-blog.com/2009/06/23/python-random-password-generator/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 22:15:36 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Nginx]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[crypt]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[passwd]]></category>
		<category><![CDATA[python blog]]></category>
		<category><![CDATA[sha1]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=48</guid>
		<description><![CDATA[Recently i made a simple random password generator with ability to make n-length passwords with appropriate hash of this password. Especially i was interested in CRYPT to create a password for nginx passwd files. You need to make a password hashed with crypt in order to put it to any *NIX passwd file. Which has [...]]]></description>
			<content:encoded><![CDATA[<p>Recently i made a simple random password generator with ability to make n-length passwords with appropriate hash of this password. Especially i was interested in CRYPT to create a password for nginx passwd files. You need to make a password hashed with crypt in order to put it to any *NIX passwd file. Which has a format of username:password using the to_crypt function you can create password for these type of access files. You can use any salt for the passwords.</p>
<p>Heres the code fell free to use it:</p>
<pre class="brush:python">'''
@author: marcink
This is a simple class for generating password from
different sets of characters

functions sha1,md5 and crypt returns a tuple of (generated_password,hashed_password,type)

'''

from hashlib import sha1, md5
import random
import crypt

class PasswordGenerator(object):

    def __init__(self, passwd = ''):

        self.ALPHABETS_NUM = r'''1234567890'''#[0]
        self.ALPHABETS_SMALL = r'''qwertyuiopasdfghjklzxcvbnm'''#[1]
        self.ALPHABETS_BIG = r'''QWERTYUIOPASDFGHJKLZXCVBNM'''#[2]
        self.ALPHABETS_SPECIAL = r'''`-=[]\;',./~!@#$%^&amp;*()_+{}|:"&lt;&gt;?'''    #[3]
        self.ALPHABETS_FULL = self.ALPHABETS_BIG + self.ALPHABETS_SMALL + self.ALPHABETS_NUM + self.ALPHABETS_SPECIAL#[4]
        self.ALPHABETS_ALPHANUM = self.ALPHABETS_BIG + self.ALPHABETS_SMALL + self.ALPHABETS_NUM#[5]
        self.ALPHABETS_BIG_SMALL = self.ALPHABETS_BIG + self.ALPHABETS_SMALL
        self.ALPHABETS_ALPHANUM_BIG = self.ALPHABETS_BIG + self.ALPHABETS_NUM#[6]
        self.ALPHABETS_ALPHANUM_SMALL = self.ALPHABETS_SMALL + self.ALPHABETS_NUM#[7]
        self.passwd = passwd

    def gen_password(self, len, type):
        #''.join(seq) is 30x faster that str_password+=random.choice(type)
        self.passwd = ''.join([random.choice(type) for _ in xrange(len) ])
        return self

    def to_md5(self):
        return (self.passwd, md5(self.passwd).hexdigest(),'md5')

    def to_sha1(self):
        return (self.passwd, sha1(self.passwd).hexdigest(),'sha1')

    def to_crypt(self, salt):
        return (self.passwd, crypt.crypt(self.passwd, salt),'crypt[salt=%s]' % salt)

    def to_plain(self):
        return self.passwd

if __name__ == "__main__":
    passwd_gen = PasswordGenerator()

    #print 8-letter password containing only big and small letters of alphabet
    print passwd_gen.gen_password(8, passwd_gen.ALPHABETS_BIG_SMALL).to_plain()

    #print 8 letter password containing numbers only and return crypt hash of it
    print passwd_gen.gen_password(8, passwd_gen.ALPHABETS_NUM).to_crypt(salt = 'ab')

    # create an instance of passwordGenerator with you own password
    my_custom_password = PasswordGenerator('my_secret_password')
    print my_custom_password.to_plain()

    #print md5 of that password
    print my_custom_password.to_md5()

    '''
    sample output:
    raeBvpdd
    ('10047981', 'abOaHGCbrkA7U', 'crypt[salt=ab]')
    my_secret_password
    ('my_secret_password', '7ee416d527a2d047751c026d82dbbeef', 'md5')
    '''</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2009/06/23/python-random-password-generator/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
