Last active
January 2, 2016 05:39
-
-
Save FunnyMan3595/8258825 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
typable_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?" | |
import sys, hmac, hashlib, base64, getpass | |
from Crypto.Random import random | |
def mkpass(len): | |
passwd = "" | |
for i in range(len): | |
passwd += random.choice(typable_chars) | |
return passwd | |
def inject(passwd, raw_inject_index, reserved, seed, pwlen, start_char, chars): | |
offset = seed % pwlen | |
for raw_look_index in range(pwlen - reserved): | |
look_index = (raw_look_index + seed + reserved) % pwlen | |
if start_char <= ord(passwd[look_index]) < start_char + chars: | |
return passwd | |
inject_index = (raw_inject_index + seed) % pwlen | |
orig = ord(passwd[inject_index]) | |
replacement = start_char + (orig + seed) % chars | |
list_passwd = list(passwd) | |
list_passwd[inject_index] = chr(replacement) | |
return "".join(list_passwd) | |
def restrict(passwd, seed, start_char, chars, filter=None): | |
list_passwd = list(passwd) | |
for index in range(len(passwd)): | |
orig = ord(passwd[index]) | |
if filter is not None: | |
if filter(orig): | |
continue | |
elif start_char <= orig < start_char + chars: | |
continue | |
replacement = (seed + index) % chars + start_char | |
list_passwd[index] = chr(replacement) | |
return "".join(list_passwd) | |
def hashpass(key, tag, pwlen=8, options="dpc"): | |
base = base64.b64encode(hmac.new(key, tag, hashlib.sha1).digest()) | |
#print "Raw: " + base | |
passwd = base | |
if options: | |
seed = sum(map(ord, base.rstrip("="))) | |
if "D" in options: | |
passwd = restrict(passwd, seed, 48, 10) | |
if "P" in options: | |
isalnum = lambda c: chr(c).isalnum() | |
passwd = restrict(passwd, seed, 65, 26, isalnum) | |
if "d" in options: | |
passwd = inject(passwd, 0, 4, seed, pwlen, 48, 10) | |
if "p" in options: | |
passwd = inject(passwd, 1, 4, seed, pwlen, 33, 15) | |
if "c" in options: | |
passwd = inject(passwd, 2, 4, seed, pwlen, 65, 26) | |
passwd = inject(passwd, 3, 4, seed, pwlen, 97, 26) | |
return passwd[:pwlen] | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print "Your password is: " + mkpass(8) | |
elif sys.argv[1] == "-h": | |
key = getpass.getpass("Master key: ") | |
tag = raw_input("Tag: ") | |
while tag: | |
options = "dpc" | |
pwlen = 8 | |
if "!" in tag: | |
tag, options = tag.split("!") | |
if "," in options: | |
options, pwlen = options.split(",") | |
pwlen = int(pwlen) | |
passwd = hashpass(key, tag, pwlen, options) | |
print "Your password is: " + passwd | |
tag = raw_input("Tag: ") | |
else: | |
print "Huh?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment