Created
November 3, 2011 02:23
-
-
Save dckc/1335610 to your computer and use it in GitHub Desktop.
Supergenpass algorithm in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Dan Connolly' | |
__contact__ = 'http://www.madmode.com/' | |
__license__ = 'dedicated to the public domain, aka CC0' | |
from base64 import b64encode | |
from hashlib import md5 | |
import re | |
def gp2_generate_passwd(Passwd, Len): | |
''' | |
>>> gp2_generate_passwd('abc' + ':' + 'google.com', 10) | |
'jZDs3ya7xD' | |
''' | |
i = 0 | |
while i < 10 or not gp2_check_passwd(Passwd[:Len]): | |
Passwd = b64_md5(Passwd) | |
i += 1 | |
return Passwd[:Len] | |
def gp2_check_passwd(Passwd): | |
''' | |
fails to start with lowercase: | |
>>> gp2_check_passwd('N4itI90rEFaPk1Jvkf3BPgAA'[:10]) | |
False | |
fails to contain a digit: | |
>>> gp2_check_passwd('xprtwflIodwvOYgsw8JD3AAA'[:10]) | |
False | |
>>> gp2_check_passwd('2JUcGu9hA8NkX0UL77REjgAA'[:10]) | |
False | |
>>> gp2_check_passwd('IvkBoBLDReR1YfZeRCUjqAAA'[:10]) | |
False | |
>>> gp2_check_passwd('5hcPWN9KmrCY0luFho8wYQAA'[:10]) | |
False | |
Finally, check passes: | |
>>> gp2_check_passwd('jZDs3ya7xDQKEVZ4DDzSXgAA'[:10]) | |
True | |
''' | |
def search(pattern): | |
m = re.search(pattern, Passwd) | |
return -1 if m is None else m.start(0) | |
return (search('[a-z]') == 0 | |
and search('[0-9]') > 0 | |
and search('[A-Z]') > 0) | |
def b64_md5(Passwd): | |
r''' | |
>>> p = 'abc:google.com' | |
>>> for i in range(0, 17): | |
... p = b64_md5(p) | |
... print i, repr(p) | |
0 'JasE6Az8W8UQsq4No2wjCQAA' | |
1 '4Jy6chsnepsYtHDjZf9iNQAA' | |
2 'qB9yfmG2Arg7EEL38kwV6wAA' | |
3 'dTSZtVtmlB9NIomTZSBzZwAA' | |
4 'XjP6oN5WsuJ89IfevhpLZQAA' | |
5 'o5J7O1J8lkIhGg9xWcerKwAA' | |
6 'Wr28EzVsGEHqt14AHpi1PgAA' | |
7 'hsQ1IjYeo0GMnCFkOeWB4AAA' | |
8 '5P9Kx08wg60RUFzLZItimgAA' | |
9 'T8hb4nOhC2w8ZlZzm4b9mAAA' | |
10 'N4itI90rEFaPk1Jvkf3BPgAA' | |
11 'xprtwflIodwvOYgsw8JD3AAA' | |
12 '2JUcGu9hA8NkX0UL77REjgAA' | |
13 'OeMSjl48mn0Ook0dwEpjcwAA' | |
14 'IvkBoBLDReR1YfZeRCUjqAAA' | |
15 '5hcPWN9KmrCY0luFho8wYQAA' | |
16 'jZDs3ya7xDQKEVZ4DDzSXgAA' | |
''' | |
# Using 'A' rather than '=' for padding seems to fall out | |
# of the base64 algorithm used. But I don't know why they use 98 | |
# rather than +/ for the altchars. | |
return b64encode(md5(Passwd).digest(), '98').replace('=', 'A') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment