diff options
author | Eli Collins <elic@assurancetechnologies.com> | 2012-04-10 16:10:05 -0400 |
---|---|---|
committer | Eli Collins <elic@assurancetechnologies.com> | 2012-04-10 16:10:05 -0400 |
commit | 157d4806512b2586c1a0fd5ee57e8c167e506f3e (patch) | |
tree | a7491056a7b51d93d17c5d3521383457d5638c07 /passlib/utils | |
parent | 75758ab6138a01ef58d75a0b4cb4c172248d9d8e (diff) | |
download | passlib-157d4806512b2586c1a0fd5ee57e8c167e506f3e.tar.gz |
replaced some common string ops with util funcs (repeat_string, right_pad_string)
Diffstat (limited to 'passlib/utils')
-rw-r--r-- | passlib/utils/__init__.py | 23 | ||||
-rw-r--r-- | passlib/utils/_blowfish/base.py | 8 |
2 files changed, 20 insertions, 11 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index 785afe0..d2fbd3f 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -481,14 +481,27 @@ def int_to_bytes(value, count): for s in irange(8*count-8,-8,-8) ) -def repeat_bytes(source, size): - "repeat or truncate <source> bytes, so it has length <size>" +def repeat_string(source, size): + "repeat or truncate <source> string, so it has length <size>" cur = len(source) - if size <= cur: - return source[:size] - else: + if size > cur: mult = (size+cur-1)//cur return (source*mult)[:size] + else: + return source[:size] + +_BNULL = b("\x00") +_UNULL = u("\x00") + +def right_pad_string(source, size, pad=None): + "right-pad or truncate <source> string, so it has length <size>" + cur = len(source) + if size > cur: + if pad is None: + pad = _UNULL if isinstance(source, unicode) else _BNULL + return source+pad*(size-cur) + else: + return source[:size] #============================================================================= # encoding helpers diff --git a/passlib/utils/_blowfish/base.py b/passlib/utils/_blowfish/base.py index 9be048b..fdbead1 100644 --- a/passlib/utils/_blowfish/base.py +++ b/passlib/utils/_blowfish/base.py @@ -5,6 +5,7 @@ #core import struct #pkg +from passlib.utils import repeat_string #local __all__ = [ "BlowfishEngine", @@ -328,12 +329,7 @@ class BlowfishEngine(object): return [0]*size # repeat data until it fills up 4*size bytes - needed = size<<2 - if dlen > needed: - data = data[:needed] - elif dlen < needed: - count = (needed+dlen-1)//dlen - data = (data * count)[:needed] + data = repeat_string(data, size<<2) # unpack return struct.unpack(">%dI" % (size,), data) |