Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Pronouncable password generator, by Joe Koberg jkoberg@gmail.com
- # based on code from http://www.multicians.org/thvv/gpw.html
- #
- import random
- class FoundStartingPointException(Exception): pass
- class PasswordGenerator:
- alphabet = 'abcdefghijklmnopqrstuvwxyz'
- def __init__(self, weightdata):
- self.data = weightdata
- self.sigma = self.computeSigma(weightdata)
- self.rnd = random.Random()
- self.rnd.seed()
- def computeSigma(self, tris):
- trisigma = 0
- for chunk in tris:
- for row in chunk:
- for col in row:
- trisigma += col
- return trisigma
- def getSigma(self):
- return self.sigma
- def generatePasswords(self, passwordLength=8, numPasswords=10):
- return [self.generatePassword(passwordLength) for i in range(numPasswords)]
- def generatePassword(self, passwordLength=8):
- password = ''
- # pick a random starting point
- r = self.rnd.random()
- rno = r * self.getSigma()
- sum = 0
- breakout = 0
- try:
- for i in range(26):
- for j in range(26):
- for k in range(26):
- sum += self.data[i][j][k]
- if sum > rno:
- password = password + self.alphabet[i] + self.alphabet[j] + self.alphabet[k]
- raise FoundStartingPointException
- except FoundStartingPointException:
- pass
- while len(password) < passwordLength + 2:
- c0 = self.alphabet.find(password[-2])
- c1 = self.alphabet.find(password[-1])
- sum1 = 0
- for c2 in range(26):
- sum1 += self.data[c0][c1][c2]
- if sum1 == 0:
- # The only time this usually happens is when it generates "-ify"
- "__main__":
- print generatePassword()
Add Comment
Please, Sign In to add comment