From 8a7267beeb3435fa16c06d22c201650e1e593d89 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 1 Nov 2013 00:37:11 -0700 Subject: [PATCH] Eliminate all uses of b'' syntax to allow for Python 2.5 support --- paramiko/common.py | 16 ++++++---------- paramiko/ecdsakey.py | 11 +++-------- paramiko/py3compat.py | 4 +++- paramiko/rsakey.py | 30 ++++++++++-------------------- tests/test_message.py | 15 +++++---------- tests/test_packetizer.py | 21 +++++---------------- tests/test_pkey.py | 7 ++----- tests/test_transport.py | 2 +- 8 files changed, 35 insertions(+), 71 deletions(-) diff --git a/paramiko/common.py b/paramiko/common.py index 5d7ab31..37d5ee8 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -123,19 +123,15 @@ else: import logging PY22 = False +zero_byte = byte_chr(0) +one_byte = byte_chr(1) +four_byte = byte_chr(4) +max_byte = byte_chr(0xff) +newline_byte = byte_chr(10) + if PY3: - zero_byte = b'\x00' - one_byte = b'\x01' - four_byte = b'\x04' - max_byte = b'\xff' - newline_byte = b'\n' cr_byte = 13 else: - zero_byte = '\x00' - one_byte = '\x01' - four_byte = '\x04' - max_byte = '\xff' - newline_byte = '\n' cr_byte = '\r' diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index d15b2ac..8585e6f 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -21,6 +21,7 @@ L{ECDSAKey} """ import binascii +from binascii import unhexlify from ecdsa import SigningKey, VerifyingKey, der, curves from ecdsa.util import number_to_string, sigencode_string, sigencode_strings, sigdecode_strings from Crypto.Hash import SHA256, MD5 @@ -157,14 +158,8 @@ class ECDSAKey (PKey): data = self._read_private_key('EC', file_obj, password) self._decode_key(data) - if PY3: - ALLOWED_PADDINGS = [b'\x01', b'\x02\x02', b'\x03\x03\x03', b'\x04\x04\x04\x04', - b'\x05\x05\x05\x05\x05', b'\x06\x06\x06\x06\x06\x06', - b'\x07\x07\x07\x07\x07\x07\x07'] - else: - ALLOWED_PADDINGS = ['\x01', '\x02\x02', '\x03\x03\x03', '\x04\x04\x04\x04', - '\x05\x05\x05\x05\x05', '\x06\x06\x06\x06\x06\x06', - '\x07\x07\x07\x07\x07\x07\x07'] + ALLOWED_PADDINGS = [one_byte, byte_chr(2) * 2, byte_chr(3) * 3, byte_chr(4) * 4, + byte_chr(5) * 5, byte_chr(6) * 6, byte_chr(7) * 7] def _decode_key(self, data): s, padding = der.remove_sequence(data) if padding: diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index fd6488d..8a01ba0 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,6 +1,6 @@ import sys -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] +__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] PY3 = sys.version_info[0] >= 3 @@ -9,6 +9,7 @@ if PY3: import struct string_types = str text_type = str + bytes = bytes bytes_types = bytes integer_types = int long = int @@ -67,6 +68,7 @@ else: string_types = basestring text_type = unicode bytes_types = str + bytes = str integer_types = (int, long) long = long input = raw_input diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index 9094503..4bc94e0 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -20,6 +20,7 @@ L{RSAKey} """ +from binascii import unhexlify from Crypto.PublicKey import RSA from Crypto.Hash import SHA, MD5 from Crypto.Cipher import DES3 @@ -152,26 +153,15 @@ class RSAKey (PKey): ### internals... - if PY3: - def _pkcs1imify(self, data): - """ - turn a 20-byte SHA1 hash into a blob of data as large as the key's N, - using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. - """ - SHA1_DIGESTINFO = b'\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14' - size = len(util.deflate_long(self.n, 0)) - filler = b'\xff' * (size - len(SHA1_DIGESTINFO) - len(data) - 3) - return b'\x00\x01' + filler + b'\x00' + SHA1_DIGESTINFO + data - else: - def _pkcs1imify(self, data): - """ - turn a 20-byte SHA1 hash into a blob of data as large as the key's N, - using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. - """ - SHA1_DIGESTINFO = b('\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14') - size = len(util.deflate_long(self.n, 0)) - filler = b('\xff') * (size - len(SHA1_DIGESTINFO) - len(data) - 3) - return b('\x00\x01') + filler + b('\x00') + SHA1_DIGESTINFO + b(data) + def _pkcs1imify(self, data): + """ + turn a 20-byte SHA1 hash into a blob of data as large as the key's N, + using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. + """ + SHA1_DIGESTINFO = unhexlify('3021300906052b0e03021a05000414') + size = len(util.deflate_long(self.n, 0)) + filler = max_byte * (size - len(SHA1_DIGESTINFO) - len(data) - 3) + return zero_byte + one_byte + filler + zero_byte + SHA1_DIGESTINFO + data def _from_private_key_file(self, filename, password): data = self._read_private_key_file('RSA', filename, password) diff --git a/tests/test_message.py b/tests/test_message.py index 2580115..dde694f 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -21,22 +21,17 @@ Some unit tests for ssh protocol message blocks. """ import unittest +from binascii import unhexlify from paramiko.message import Message from paramiko.common import * class MessageTest (unittest.TestCase): - if PY3: - __a = b'\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01q\x00\x00\x00\x05hello\x00\x00\x03\xe8' + (b'x' * 1000) - __b = b'\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' - __c = b'\x00\x00\x00\x05\xff\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' - __d = b'\x00\x00\x00\x05\xff\x00\x00\x00\x05\x11\x22\x33\x44\x55\xff\x00\x00\x00\x0a\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03cat\x00\x00\x00\x03a,b' - else: - __a = '\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01q\x00\x00\x00\x05hello\x00\x00\x03\xe8' + ('x' * 1000) - __b = '\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' - __c = '\x00\x00\x00\x05\xff\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' - __d = '\x00\x00\x00\x05\xff\x00\x00\x00\x05\x11\x22\x33\x44\x55\xff\x00\x00\x00\x0a\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03cat\x00\x00\x00\x03a,b' + __a = unhexlify('000000170760e09000000001710000000568656c6c6f000003e8') + (b('x') * 1000) + __b = unhexlify('0100f3003f00000010687565792c64657765792c6c6f756965') + __c = unhexlify('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7') + __d = unhexlify('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62') def test_1_encode(self): msg = Message() diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index 111ecb9..b0e8fd4 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -21,18 +21,15 @@ Some unit tests for the ssh2 protocol in Transport. """ import unittest +from binascii import unhexlify from tests.loop import LoopSocket from Crypto.Cipher import AES from Crypto.Hash import SHA, HMAC from paramiko import Message, Packetizer, util from paramiko.common import * -if PY3: - x55 = b'\x55' - x1f = b'\x1f' -else: - x55 = '\x55' - x1f = '\x1f' +x55 = byte_chr(0x55) +x1f = byte_chr(0x1f) class PacketizerTest (unittest.TestCase): @@ -58,10 +55,7 @@ class PacketizerTest (unittest.TestCase): data = rsock.recv(100) # 32 + 12 bytes of MAC = 44 self.assertEquals(44, len(data)) - if PY3: - self.assertEquals(b'\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) - else: - self.assertEquals('\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) + self.assertEquals(unhexlify('439197bd5b50ac2587c2c46bc7e938c0'), data[:16]) def test_2_read (self): rsock = LoopSocket() @@ -72,12 +66,7 @@ class PacketizerTest (unittest.TestCase): p.set_hexdump(True) cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16) p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20) - if PY3: - wsock.send(b'C\x91\x97\xbd[P\xac%\x87\xc2\xc4k\xc7\xe98\xc0' + \ - b'\x90\xd2\x16V\rqsa8|L=\xfb\x97}\xe2n\x03\xb1\xa0\xc2\x1c\xd6AAL\xb4Y') - else: - wsock.send('C\x91\x97\xbd[P\xac%\x87\xc2\xc4k\xc7\xe98\xc0' + \ - '\x90\xd2\x16V\rqsa8|L=\xfb\x97}\xe2n\x03\xb1\xa0\xc2\x1c\xd6AAL\xb4Y') + wsock.send(unhexlify('439197bd5b50ac2587c2c46bc7e938c090d216560d717361387c4c3dfb977de26e03b1a0c21cd641414cb459')) cmd, m = p.read_message() self.assertEquals(100, cmd) self.assertEquals(100, m.get_int()) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index b75f9a1..0000160 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -23,7 +23,7 @@ Some unit tests for public/private key objects. from binascii import hexlify, unhexlify import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng, StringIO, byte_chr, b, PY3 +from paramiko.common import rng, StringIO, byte_chr, b, PY3, bytes from tests.util import test_path # from openssh's ssh-keygen @@ -77,10 +77,7 @@ ADRvOqQ5R98Sxst765CAqXmRtz8vwoD96g== -----END EC PRIVATE KEY----- """ -if PY3: - x1234 = b'\x01\x02\x03\x04' -else: - x1234 = '\x01\x02\x03\x04' +x1234 = unhexlify('01020304') class KeyTest (unittest.TestCase): diff --git a/tests/test_transport.py b/tests/test_transport.py index 230f4a2..f60c294 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -33,7 +33,7 @@ from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey SSHException, BadAuthenticationType, InteractiveQuery, ChannelException from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED -from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST, b +from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST, b, bytes from paramiko.message import Message from tests.loop import LoopSocket from tests.util import ParamikoTest, test_path