Skip to content

Commit

Permalink
Python3 port
Browse files Browse the repository at this point in the history
  • Loading branch information
manns committed Jul 28, 2018
1 parent 7f357e1 commit 1d1c43a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
16 changes: 8 additions & 8 deletions passwordmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from pwmlib import *

def gui():
import Tkinter as tk
import tkinter as tk

class Application(tk.Frame):

Expand Down Expand Up @@ -69,7 +69,7 @@ def createWidgets(self):
self.alg_label = tk.Label(self, justify="left", text="Algorithm")
self.alg = tk.StringVar(self)
self.alg.set(settings.Algorithm)
self.alg_combo = apply(tk.OptionMenu, (self, self.alg) + tuple(self.PWmaker.valid_algs))
self.alg_combo = tk.OptionMenu(*(self, self.alg) + tuple(self.PWmaker.valid_algs))
self.user_label = tk.Label(self, justify="left", text="Username")
self.user_text = tk.Entry(self)
self.user_text.insert(0, settings.Username)
Expand Down Expand Up @@ -145,9 +145,9 @@ def load(self):
def generate(self):
self.generate_button.flash()
try:
print self.getsettings()
print(self.getsettings())
pw = self.PWmaker.generatepasswordfrom(self.getsettings())
except PWM_Error, e:
except PWM_Error as e:
pw = str(e)
current_passwd = self.passwd_text.get()
if len(current_passwd) > 0:
Expand Down Expand Up @@ -186,7 +186,7 @@ def cmd():
leetlevel = 0
try:
PWmaker = PWM()
print PWmaker.generatepassword(options.alg,
print(PWmaker.generatepassword(options.alg,
options.mpw,
options.url + options.user + options.mod,
leet,
Expand All @@ -195,9 +195,9 @@ def cmd():
options.charset,
options.pfx,
options.sfx,
)
except PWM_Error, e:
print e
))
except PWM_Error as e:
print(e)
sys.exit(1)


Expand Down
47 changes: 31 additions & 16 deletions pwmlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,28 @@ def generatepassword(self,hashAlgorithm, key, data, whereToUseL33t, l33tLevel, p


class PWM_HashUtils:
def rstr2any(self, input, encoding, trim=True):
"""Convert a raw string to an arbitrary string encoding. Set trim
to false for keeping leading zeros"""
def rstr2any(self, inp, encoding, trim=True):
"""Convert a raw string to an arbitrary string encoding.
Set trim to false for keeping leading zeros
"""

divisor = len(encoding)
remainders = []

# Convert to an array of 16-bit big-endian values, forming the dividend
dividend = []
# pad this
while len(dividend) < math.ceil(len(input) / 2):
while len(dividend) < math.ceil(len(inp) / 2):
dividend.append(0)
inp = input # Because Miquel is a lazy twit and didn't want to do a search and replace

for i in range(len(dividend)):
dividend[i] = (ord(inp[i * 2]) << 8) | ord(inp[i * 2 + 1])
print(dividend)
print(inp, encoding, trim)
print(repr(chr(inp[i * 2])))
print(chr(inp[i * 2]))
dividend[i] = (inp[i * 2] << 8) | inp[i * 2 + 1]

# Repeatedly perform a long division. The binary array forms the dividend,
# the length of the encoding is the divisor. Once computed, the quotient
Expand All @@ -175,7 +183,7 @@ def rstr2any(self, input, encoding, trim=True):
remainders.append(x)
dividend = quotient
else:
full_length = math.ceil(float(len(input) * 8) / (math.log(len(encoding)) / math.log(2)))
full_length = math.ceil(float(len(inp) * 8) / (math.log(len(encoding)) / math.log(2)))
for j in range(len(full_length)):
quotient = []
x = 0
Expand All @@ -193,16 +201,19 @@ def rstr2any(self, input, encoding, trim=True):
for i in reversed(remainders):
output += encoding[i]

print(inp, encoding, trim, output)

return output

def any_md5(self, s, e, t):
s = s.encode("utf-8")
if float(sys.version[:3]) >= 2.5:
import hashlib
hash = hashlib.md5(s).digest()
__hash = hashlib.md5(s).digest()
else:
import md5
hash = md5.new(s).digest()
return self.rstr2any(hash, e, t)
__hash = md5.new(s).digest()
return self.rstr2any(__hash, e, t)

def any_hmac_md5(self, k, d, e, t):
if float(sys.version[:3]) >= 2.5:
Expand All @@ -214,13 +225,14 @@ def any_hmac_md5(self, k, d, e, t):
return self.rstr2any(hmac.new(k, d, hashfunc).digest(), e, t)

def any_sha1(self, s, e, t):
s = s.encode("utf-8")
if float(sys.version[:3]) >= 2.5:
import hashlib
hash = hashlib.sha1(s).digest()
__hash = hashlib.sha1(s).digest()
else:
import sha
hash = sha.new(s).digest()
return self.rstr2any(hash, e, t)
__hash = sha.new(s).digest()
return self.rstr2any(__hash, e, t)

def any_hmac_sha1(self, k, d, e, t):
if float(sys.version[:3]) >= 2.5:
Expand All @@ -232,13 +244,14 @@ def any_hmac_sha1(self, k, d, e, t):
return self.rstr2any(hmac.new(k, d, hashfunc).digest(), e, t)

def any_sha256(self, s, e, t):
s = s.encode("utf-8")
if float(sys.version[:3]) >= 2.5:
import hashlib
hash = hashlib.sha256(s).digest()
__hash = hashlib.sha256(s).digest()
else:
from Crypto.Hash import SHA256
hash = SHA256.new(s).digest()
return self.rstr2any(hash, e, t)
__hash = SHA256.new(s).digest()
return self.rstr2any(__hash, e, t)

def any_hmac_sha256(self, k, d, e, t):
if float(sys.version[:3]) >= 2.5:
Expand All @@ -250,6 +263,7 @@ def any_hmac_sha256(self, k, d, e, t):
return self.rstr2any(hmac.new(k, d, hashfunc).digest(), e, t)

def any_md4(self, s, e, t):
s = s.encode("utf-8")
from Crypto.Hash import MD4
return self.rstr2any(MD4.new(s).digest(), e, t)

Expand All @@ -258,6 +272,7 @@ def any_hmac_md4(self, k, d, e, t):
return self.rstr2any(hmac.new(k, d, Crypto.Hash.MD4).digest(), e, t)

def any_rmd160(self, s, e, t):
s = s.encode("utf-8")
from Crypto.Hash import RIPEMD
return self.rstr2any(RIPEMD.new(s).digest(), e, t)

Expand Down

0 comments on commit 1d1c43a

Please sign in to comment.