Skip to content

Instantly share code, notes, and snippets.

@agustingianni
Created November 8, 2009 21:24
Show Gist options
  • Save agustingianni/229480 to your computer and use it in GitHub Desktop.
Save agustingianni/229480 to your computer and use it in GitHub Desktop.
Generador de passwords usando Cadenas de Markov
import numpy
import sys
import string
import random
class Markov:
def __init__(self):
self.P = {}
def Parse(self, filename):
for line in open(filename):
word = line.split()[-2]
char1 = char2 = ""
for current_char in word:
self.P.setdefault((char1, char2), []).append(current_char)
char1 = char2
char2 = current_char
def Generate(self, N=1000, lenght=None):
# Si no se especifica ningun tamanio en particular
# vamos generar el tamanio de cada password de acuerdo
# a la distribucion empirica que obtuvimos analizando
# la muestra de 400k passwords
mu = 6
sigma = 2
if lenght == None:
lenght = map(int, numpy.random.normal(mu, sigma, N))
else:
lenght = [lenght] * N
G = []
for i in xrange(0, N):
password = ""
c1, c2 = random.choice(self.P.keys())
for i in xrange(lenght[i]):
try:
c3 = random.choice(self.P[(c1, c2)])
except KeyError:
c1, c2 = random.choice(self.P.keys())
c3 = c1
password += c3
c1 = c2
c2 = c3
G.append(password)
return G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment