summaryrefslogtreecommitdiff
path: root/passlib/utils
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-01-25 01:17:40 +0000
committerEli Collins <elic@assurancetechnologies.com>2011-01-25 01:17:40 +0000
commit2ba159bb201d3da7fe5fcd533e1a6531a3489062 (patch)
tree6f2a3e51150d9c729f3695d36688a7f1637da89d /passlib/utils
parent5e9e909043cab4133443c574ef4318bb229e87ec (diff)
downloadpasslib-2ba159bb201d3da7fe5fcd533e1a6531a3489062.tar.gz
work on lanman hash
Diffstat (limited to 'passlib/utils')
-rw-r--r--passlib/utils/__init__.py23
-rw-r--r--passlib/utils/_slow_des_crypt.py49
-rw-r--r--passlib/utils/md4.py1
3 files changed, 71 insertions, 2 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index dd17d1d..9b8a472 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -73,8 +73,29 @@ Undef = object() #singleton used as default kwd value in some functions
#numeric helpers
#=================================================================================
-#TODO: rename 'bytes' kwd for py30 compat purposes
+##def int_to_bytes(value, count=None):
+## """encode a integer into a string of bytes"""
+##
+##def bytes_to_int(value, order="big"):
+## """decode a byte string into an integer representation of it's binary value.
+##
+## :arg value: the string to decode.
+## :param order: the byte ordering; "big" (the default), "little", or "native"
+##
+## :returns: the decoded positive integer.
+## """
+## if not value:
+## return 0
+## if order == "native":
+## order = sys.byteorder
+## if order == "little":
+## value = reversed(value)
+## out = 0
+## for v in value:
+## out = (out<<8) | ord(v)
+## return out
+#TODO: rename 'bytes' kwd for py30 compat purposes
def list_to_bytes(value, bytes=None, order="big"):
"""Returns a multi-character string corresponding to a list of byte values.
diff --git a/passlib/utils/_slow_des_crypt.py b/passlib/utils/_slow_des_crypt.py
index cc68327..9ebf518 100644
--- a/passlib/utils/_slow_des_crypt.py
+++ b/passlib/utils/_slow_des_crypt.py
@@ -620,8 +620,55 @@ def permute(c, p):
c >>= 4
return out
+def bytes_to_int(value):
+ out = 0
+ for v in value:
+ out = (out<<8) | ord(v)
+ return out
+
+def int_to_bytes(value, count):
+ return ''.join(
+ chr((value>>s) & 0xff)
+ for s in xrange(8*count-8,-8,-8)
+ )
+
+def des_encrypt_block(key, input):
+ "do traditional encryption of a single DES block"
+ assert len(input) == 8
+ assert len(key) == 8
+ input = bytes_to_int(input)
+ key = bytes_to_int(key)
+ out = des_encrypt_rounds(input, 0, 1, key)
+ return int_to_bytes(out, 8)
+
+def expand_des_key(source):
+ "convert 7 byte des key to 8 byte des key (by adding parity bit every 7 bits)"
+ #NOTE: could probably do this much more cleverly and efficiently,
+ # but no need really given it's use
+ assert len(source) == 7
+
+ def iter_bits(source):
+ for c in source:
+ v = ord(c)
+ for i in xrange(7,-1,-1):
+ yield (v>>i) & 1
+
+ out = 0
+ p = 1
+ for i, b in enumerate(iter_bits(source)):
+ out = (out<<1) + b
+ p ^= b
+ if i % 7 == 6:
+ out = (out<<1) + p
+ p = 1
+
+ return ''.join(
+ chr((out>>s) & 0xFF)
+ for s in xrange(8*7,-8,-8)
+ )
+
def des_encrypt_rounds(input, salt, rounds, key):
- """Returns modified DES for single block of input"""
+ """returns modified DES for single block of input, used by des-crypt algorithm"""
global SPE, PCXROT, IE3264, CF6464
#bounds check
diff --git a/passlib/utils/md4.py b/passlib/utils/md4.py
index fe88f6a..2eb2c95 100644
--- a/passlib/utils/md4.py
+++ b/passlib/utils/md4.py
@@ -27,6 +27,7 @@ class md4(object):
"md4 hash algorithm"
#FIXME: make this follow hash object PEP better.
#FIXME: this isn't threadsafe
+ #XXX: should we monkeypatch ourselves into hashlib for general use? probably wouldn't be nice.
digest_size = digestsize = 16