#!/usr/bin/python
# ______________________________________________________________________
# Quick Password version 1.1
# Copyright (C) 2002 Dan Grassi
# Author: Dan Grassi <Dan@Grassi.org>
#
# This source is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation, version 2.
#
# If you use and/or modify this code please email the author and
# provide an URL where the updated program code can be obtained.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You can retrieve a copy of the GNU Library General Public License
# from http://www.gnu.org/. For a copy via US Mail, write to the
#
# Free Software Foundation, Inc.
# 59 Temple Place - Suite 330,
# Boston, MA 02111-1307
# USA
# ______________________________________________________________________
import sys
import random
import string
# ______________________________________________________________________
def GetWord(dictFilePath = "/usr/share/dict/words", minWordLength=4, maxWordLength=6):
'''
This function reads a dictionary, extracts a word at random
ignoring words that start with an uppercase letter (Proper names.)
The Parameters are as follows:
dictFilePath Path to the dictionary default = /usr/share/dict/words
minWordLength Minimum character length of the words default = 4
maxWordLength Maximum character length of the words default = 6
'''
# Must be at least twice the size of a word in the password dictionary
kMargin = 100
try:
inFile = open(dictFilePath, 'r')
inFile.seek(0, 2)
fileSize = inFile.tell() - kMargin
for i in range(1, 1000):
pointer = random.randint(0, fileSize-kMargin)
inFile.seek(pointer)
word = inFile.readline() # probably does not start on a word boundry
word = inFile.readline()[:-1]
if ((minWordLength <= len(word) <= maxWordLength) and (string.lower(word[0]) == word[0])):
break
inFile.close()
except:
word = "Error in GetWord"
return word
# ______________________________________________________________________
def GetPass(wordCount=2, minWordLength=4, maxWordLength=6, dictFilePath = "/usr/share/dict/words"):
'''
This function reads a dictionary, extracts words at random
and joins them with a seperator character.
The Parameters are as follows:
wordCount Number od words to join together default = 2
minWordLength Minimum character length of the words default = 4
maxWordLength Maximum character length of the words default = 6
dictFilePath Path to the dictionary default = "/usr/share/dict/words"
'''
# Legal seperator characters
seperators = "!#%)*+-23456789=]"
try:
pw = GetWord(dictFilePath, minWordLength, maxWordLength)
for i in range(1, wordCount):
pw = pw + random.choice(seperators) + GetWord(dictFilePath, minWordLength, maxWordLength)
except:
pw = "Error in GetPass"
return pw
# ______________________________________________________________________
def GetWordCount(dictFilePath='/usr/share/dict/words'):
wordCounts = {}
try:
inFile = open(dictFilePath, 'r')
words = inFile.readlines()
inFile.close()
for w in words:
l = len(w)-1
if wordCounts.has_key(l):
wordCounts[l] += 1
else:
wordCounts[l] = 1
except: pass
return wordCounts
# Test
# ______________________________________________________________________
if __name__ == '__main__':
print GetWordCount()
print GetPass()