ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Generate a human readable 'random' password (nicepass.py)
Submitter: Pradeep Kishore Gowda (other recipes)
Last Updated: 2005/04/26
Version no: 1.3
Category: System

 

4 stars 3 vote(s)


Description:

Password will be generated in the form 'word'+digits+'word' eg.,nice137pace
instead of a difficult-to-remember - K8Yn9muL

Source: Text Source

## Generate a human readable 'random' password
## password  will be generated in the form 'word'+digits+'word' 
## eg.,nice137pass
## parameters: number of 'characters' , number of 'digits'
## Pradeep Kishore Gowda <pradeep at btbytes.com >
## License : GPL 
## Date : 2005.April.15
## Revision 1.2 
## ChangeLog: 
## 1.1 - fixed typos 
## 1.2 - renamed functions _apart & _npart to a_part & n_part as zope does not allow functions to 
## start with _

def nicepass(alpha=6,numeric=2):
    """
    returns a human-readble password (say rol86din instead of 
    a difficult to remember K8Yn9muL ) 
    """
    import string
    import random
    vowels = ['a','e','i','o','u']
    consonants = [a for a in string.ascii_lowercase if a not in vowels]
    digits = string.digits
    
    ####utility functions
    def a_part(slen):
        ret = ''
        for i in range(slen):			
            if i%2 ==0:
                randid = random.randint(0,20) #number of consonants
                ret += consonants[randid]
            else:
                randid = random.randint(0,4) #number of vowels
                ret += vowels[randid]
        return ret
    
    def n_part(slen):
        ret = ''
        for i in range(slen):
            randid = random.randint(0,9) #number of digits
            ret += digits[randid]
        return ret
        
    #### 	
    fpl = alpha/2		
    if alpha % 2 :
        fpl = int(alpha/2) + 1 					
    lpl = alpha - fpl	
    
    start = a_part(fpl)
    mid = n_part(numeric)
    end = a_part(lpl)
    
    return "%s%s%s" % (start,mid,end)
    
if __name__ == "__main__":
    print nicepass(6,2)

Discussion:

Other receipies like:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/59873 generate a strong albeit difficult to remember password.

This script will be useful in cases where you are sending auto-generated passwords to registrants(say) over email.
This password is difficult than a dictionary word, at the same time easy enough to 'read' and remember by the recipient.



Add comment

Number of comments: 2

Nice sounding algorithm, hemanth sethuram, 2005/04/21
It's a nice recipe. Your algorithm for getting nice sounding words with random characters is to alternate between consonants and vowels (all the if i%2 == 0 statements). With this, you cannot get your example password 'nice137pass' but something like 'nice137pace'. You can also add 'y' to your vowel list to get some more pronounceable phrases. --Hemanth P.S.
Add comment

This is similar, Lorenzo Bolognini, 2005/05/07
http://www.adel.nursat.kz/apg/
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. Treat the Win32 Registry ...

4. Watching a directory ...

5. Union Find data structure

6. Function Decorators by ...

7. MS SQL Server log monitor

8. Table objects with ...

9. wx twisted support using ...

10. More accurate sum




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.