summaryrefslogtreecommitdiff
path: root/paramiko/util.py
diff options
context:
space:
mode:
authorJeff Forcier <jeff@bitprophet.org>2014-03-05 17:03:37 -0800
committerJeff Forcier <jeff@bitprophet.org>2014-03-05 17:03:37 -0800
commitb2be63ec623b5944f9b84cac8b8f41aeb2b42fb7 (patch)
tree389e17b0c08cd34872a2e3afbc34860ab44fb3ed /paramiko/util.py
parentbd61c7c0a9a4a2020d0acfb6a01e9ec85bb43b8e (diff)
parentae078f51d622931954e47e78029a889c4e721a05 (diff)
downloadparamiko-b2be63ec623b5944f9b84cac8b8f41aeb2b42fb7.tar.gz
Merge remote-tracking branch 'scottkmaxwell/py3-support-without-py25' into python3
Conflicts: dev-requirements.txt paramiko/__init__.py paramiko/file.py paramiko/hostkeys.py paramiko/message.py paramiko/proxy.py paramiko/server.py paramiko/transport.py paramiko/util.py paramiko/win_pageant.py setup.py
Diffstat (limited to 'paramiko/util.py')
-rw-r--r--paramiko/util.py81
1 files changed, 39 insertions, 42 deletions
diff --git a/paramiko/util.py b/paramiko/util.py
index e0ef3b7c..c4b87e3b 100644
--- a/paramiko/util.py
+++ b/paramiko/util.py
@@ -48,60 +48,53 @@ if sys.version_info < (2,3):
def inflate_long(s, always_positive=False):
"turns a normalized byte string into a long-int (adapted from Crypto.Util.number)"
- out = 0L
+ out = long(0)
negative = 0
- if not always_positive and (len(s) > 0) and (ord(s[0]) >= 0x80):
+ if not always_positive and (len(s) > 0) and (byte_ord(s[0]) >= 0x80):
negative = 1
if len(s) % 4:
- filler = '\x00'
+ filler = zero_byte
if negative:
- filler = '\xff'
+ filler = max_byte
s = filler * (4 - len(s) % 4) + s
for i in range(0, len(s), 4):
out = (out << 32) + struct.unpack('>I', s[i:i+4])[0]
if negative:
- out -= (1L << (8 * len(s)))
+ out -= (long(1) << (8 * len(s)))
return out
+deflate_zero = zero_byte if PY2 else 0
+deflate_ff = max_byte if PY2 else 0xff
+
def deflate_long(n, add_sign_padding=True):
"turns a long-int into a normalized byte string (adapted from Crypto.Util.number)"
# after much testing, this algorithm was deemed to be the fastest
- s = ''
+ s = bytes()
n = long(n)
while (n != 0) and (n != -1):
- s = struct.pack('>I', n & 0xffffffffL) + s
+ s = struct.pack('>I', n & xffffffff) + s
n = n >> 32
# strip off leading zeros, FFs
for i in enumerate(s):
- if (n == 0) and (i[1] != '\000'):
+ if (n == 0) and (i[1] != deflate_zero):
break
- if (n == -1) and (i[1] != '\xff'):
+ if (n == -1) and (i[1] != deflate_ff):
break
else:
# degenerate case, n was either 0 or -1
i = (0,)
if n == 0:
- s = '\000'
+ s = zero_byte
else:
- s = '\xff'
+ s = max_byte
s = s[i[0]:]
if add_sign_padding:
- if (n == 0) and (ord(s[0]) >= 0x80):
- s = '\x00' + s
- if (n == -1) and (ord(s[0]) < 0x80):
- s = '\xff' + s
+ if (n == 0) and (byte_ord(s[0]) >= 0x80):
+ s = zero_byte + s
+ if (n == -1) and (byte_ord(s[0]) < 0x80):
+ s = max_byte + s
return s
-def format_binary_weird(data):
- out = ''
- for i in enumerate(data):
- out += '%02X' % ord(i[1])
- if i[0] % 2:
- out += ' '
- if i[0] % 16 == 15:
- out += '\n'
- return out
-
def format_binary(data, prefix=''):
x = 0
out = []
@@ -113,8 +106,8 @@ def format_binary(data, prefix=''):
return [prefix + x for x in out]
def format_binary_line(data):
- left = ' '.join(['%02X' % ord(c) for c in data])
- right = ''.join([('.%c..' % c)[(ord(c)+63)//95] for c in data])
+ left = ' '.join(['%02X' % byte_ord(c) for c in data])
+ right = ''.join([('.%c..' % c)[(byte_ord(c)+63)//95] for c in data])
return '%-50s %s' % (left, right)
def hexify(s):
@@ -126,17 +119,20 @@ def unhexify(s):
def safe_string(s):
out = ''
for c in s:
- if (ord(c) >= 32) and (ord(c) <= 127):
+ if (byte_ord(c) >= 32) and (byte_ord(c) <= 127):
out += c
else:
- out += '%%%02X' % ord(c)
+ out += '%%%02X' % byte_ord(c)
return out
# ''.join([['%%%02X' % ord(c), c][(ord(c) >= 32) and (ord(c) <= 127)] for c in s])
def bit_length(n):
+ try:
+ return n.bitlength()
+ except AttributeError:
norm = deflate_long(n, 0)
- hbyte = ord(norm[0])
+ hbyte = byte_ord(norm[0])
if hbyte == 0:
return 1
bitlen = len(norm) * 8
@@ -157,20 +153,21 @@ def generate_key_bytes(hashclass, salt, key, nbytes):
:param class hashclass:
class from `Crypto.Hash` that can be used as a secure hashing function
(like ``MD5`` or ``SHA``).
- :param str salt: data to salt the hash with.
+ :param salt: data to salt the hash with.
+ :type salt: byte string
:param str key: human-entered password or passphrase.
:param int nbytes: number of bytes to generate.
:return: Key data `str`
"""
- keydata = ''
- digest = ''
+ keydata = bytes()
+ digest = bytes()
if len(salt) > 8:
salt = salt[:8]
while nbytes > 0:
hash_obj = hashclass.new()
if len(digest) > 0:
hash_obj.update(digest)
- hash_obj.update(key)
+ hash_obj.update(b(key))
hash_obj.update(salt)
digest = hash_obj.digest()
size = min(nbytes, len(digest))
@@ -271,37 +268,37 @@ def retry_on_signal(function):
while True:
try:
return function()
- except EnvironmentError, e:
+ except EnvironmentError as e:
if e.errno != errno.EINTR:
raise
class Counter (object):
"""Stateful counter for CTR mode crypto"""
- def __init__(self, nbits, initial_value=1L, overflow=0L):
+ def __init__(self, nbits, initial_value=long(1), overflow=long(0)):
self.blocksize = nbits / 8
self.overflow = overflow
# start with value - 1 so we don't have to store intermediate values when counting
# could the iv be 0?
if initial_value == 0:
- self.value = array.array('c', '\xFF' * self.blocksize)
+ self.value = array.array('c', max_byte * self.blocksize)
else:
x = deflate_long(initial_value - 1, add_sign_padding=False)
- self.value = array.array('c', '\x00' * (self.blocksize - len(x)) + x)
+ self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x)
def __call__(self):
"""Increament the counter and return the new value"""
i = self.blocksize - 1
while i > -1:
- c = self.value[i] = chr((ord(self.value[i]) + 1) % 256)
- if c != '\x00':
+ c = self.value[i] = byte_chr((byte_ord(self.value[i]) + 1) % 256)
+ if c != zero_byte:
return self.value.tostring()
i -= 1
# counter reset
x = deflate_long(self.overflow, add_sign_padding=False)
- self.value = array.array('c', '\x00' * (self.blocksize - len(x)) + x)
+ self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x)
return self.value.tostring()
- def new(cls, nbits, initial_value=1L, overflow=0L):
+ def new(cls, nbits, initial_value=long(1), overflow=long(0)):
return cls(nbits, initial_value=initial_value, overflow=overflow)
new = classmethod(new)