Eliminate all uses of b'' syntax to allow for Python 2.5 support

This commit is contained in:
Scott Maxwell 2013-11-01 00:37:11 -07:00
parent d5ce2b43d6
commit 8a7267beeb
8 changed files with 35 additions and 71 deletions

View File

@ -123,19 +123,15 @@ else:
import logging import logging
PY22 = False 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: 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 cr_byte = 13
else: else:
zero_byte = '\x00'
one_byte = '\x01'
four_byte = '\x04'
max_byte = '\xff'
newline_byte = '\n'
cr_byte = '\r' cr_byte = '\r'

View File

@ -21,6 +21,7 @@ L{ECDSAKey}
""" """
import binascii import binascii
from binascii import unhexlify
from ecdsa import SigningKey, VerifyingKey, der, curves from ecdsa import SigningKey, VerifyingKey, der, curves
from ecdsa.util import number_to_string, sigencode_string, sigencode_strings, sigdecode_strings from ecdsa.util import number_to_string, sigencode_string, sigencode_strings, sigdecode_strings
from Crypto.Hash import SHA256, MD5 from Crypto.Hash import SHA256, MD5
@ -157,14 +158,8 @@ class ECDSAKey (PKey):
data = self._read_private_key('EC', file_obj, password) data = self._read_private_key('EC', file_obj, password)
self._decode_key(data) self._decode_key(data)
if PY3: ALLOWED_PADDINGS = [one_byte, byte_chr(2) * 2, byte_chr(3) * 3, byte_chr(4) * 4,
ALLOWED_PADDINGS = [b'\x01', b'\x02\x02', b'\x03\x03\x03', b'\x04\x04\x04\x04', byte_chr(5) * 5, byte_chr(6) * 6, byte_chr(7) * 7]
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']
def _decode_key(self, data): def _decode_key(self, data):
s, padding = der.remove_sequence(data) s, padding = der.remove_sequence(data)
if padding: if padding:

View File

@ -1,6 +1,6 @@
import sys 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 PY3 = sys.version_info[0] >= 3
@ -9,6 +9,7 @@ if PY3:
import struct import struct
string_types = str string_types = str
text_type = str text_type = str
bytes = bytes
bytes_types = bytes bytes_types = bytes
integer_types = int integer_types = int
long = int long = int
@ -67,6 +68,7 @@ else:
string_types = basestring string_types = basestring
text_type = unicode text_type = unicode
bytes_types = str bytes_types = str
bytes = str
integer_types = (int, long) integer_types = (int, long)
long = long long = long
input = raw_input input = raw_input

View File

@ -20,6 +20,7 @@
L{RSAKey} L{RSAKey}
""" """
from binascii import unhexlify
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from Crypto.Hash import SHA, MD5 from Crypto.Hash import SHA, MD5
from Crypto.Cipher import DES3 from Crypto.Cipher import DES3
@ -152,26 +153,15 @@ class RSAKey (PKey):
### internals... ### internals...
if PY3:
def _pkcs1imify(self, data): def _pkcs1imify(self, data):
""" """
turn a 20-byte SHA1 hash into a blob of data as large as the key's N, 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. 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' SHA1_DIGESTINFO = unhexlify('3021300906052b0e03021a05000414')
size = len(util.deflate_long(self.n, 0)) size = len(util.deflate_long(self.n, 0))
filler = b'\xff' * (size - len(SHA1_DIGESTINFO) - len(data) - 3) filler = max_byte * (size - len(SHA1_DIGESTINFO) - len(data) - 3)
return b'\x00\x01' + filler + b'\x00' + SHA1_DIGESTINFO + data return zero_byte + one_byte + filler + zero_byte + 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 _from_private_key_file(self, filename, password): def _from_private_key_file(self, filename, password):
data = self._read_private_key_file('RSA', filename, password) data = self._read_private_key_file('RSA', filename, password)

View File

@ -21,22 +21,17 @@ Some unit tests for ssh protocol message blocks.
""" """
import unittest import unittest
from binascii import unhexlify
from paramiko.message import Message from paramiko.message import Message
from paramiko.common import * from paramiko.common import *
class MessageTest (unittest.TestCase): class MessageTest (unittest.TestCase):
if PY3: __a = unhexlify('000000170760e09000000001710000000568656c6c6f000003e8') + (b('x') * 1000)
__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 = unhexlify('0100f3003f00000010687565792c64657765792c6c6f756965')
__b = b'\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' __c = unhexlify('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7')
__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 = unhexlify('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62')
__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'
def test_1_encode(self): def test_1_encode(self):
msg = Message() msg = Message()

View File

@ -21,18 +21,15 @@ Some unit tests for the ssh2 protocol in Transport.
""" """
import unittest import unittest
from binascii import unhexlify
from tests.loop import LoopSocket from tests.loop import LoopSocket
from Crypto.Cipher import AES from Crypto.Cipher import AES
from Crypto.Hash import SHA, HMAC from Crypto.Hash import SHA, HMAC
from paramiko import Message, Packetizer, util from paramiko import Message, Packetizer, util
from paramiko.common import * from paramiko.common import *
if PY3: x55 = byte_chr(0x55)
x55 = b'\x55' x1f = byte_chr(0x1f)
x1f = b'\x1f'
else:
x55 = '\x55'
x1f = '\x1f'
class PacketizerTest (unittest.TestCase): class PacketizerTest (unittest.TestCase):
@ -58,10 +55,7 @@ class PacketizerTest (unittest.TestCase):
data = rsock.recv(100) data = rsock.recv(100)
# 32 + 12 bytes of MAC = 44 # 32 + 12 bytes of MAC = 44
self.assertEquals(44, len(data)) self.assertEquals(44, len(data))
if PY3: self.assertEquals(unhexlify('439197bd5b50ac2587c2c46bc7e938c0'), data[:16])
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])
def test_2_read (self): def test_2_read (self):
rsock = LoopSocket() rsock = LoopSocket()
@ -72,12 +66,7 @@ class PacketizerTest (unittest.TestCase):
p.set_hexdump(True) p.set_hexdump(True)
cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16) cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16)
p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20) p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20)
if PY3: wsock.send(unhexlify('439197bd5b50ac2587c2c46bc7e938c090d216560d717361387c4c3dfb977de26e03b1a0c21cd641414cb459'))
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')
cmd, m = p.read_message() cmd, m = p.read_message()
self.assertEquals(100, cmd) self.assertEquals(100, cmd)
self.assertEquals(100, m.get_int()) self.assertEquals(100, m.get_int())

View File

@ -23,7 +23,7 @@ Some unit tests for public/private key objects.
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
import unittest import unittest
from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util 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 tests.util import test_path
# from openssh's ssh-keygen # from openssh's ssh-keygen
@ -77,10 +77,7 @@ ADRvOqQ5R98Sxst765CAqXmRtz8vwoD96g==
-----END EC PRIVATE KEY----- -----END EC PRIVATE KEY-----
""" """
if PY3: x1234 = unhexlify('01020304')
x1234 = b'\x01\x02\x03\x04'
else:
x1234 = '\x01\x02\x03\x04'
class KeyTest (unittest.TestCase): class KeyTest (unittest.TestCase):

View File

@ -33,7 +33,7 @@ from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey
SSHException, BadAuthenticationType, InteractiveQuery, ChannelException SSHException, BadAuthenticationType, InteractiveQuery, ChannelException
from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL
from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED 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 paramiko.message import Message
from tests.loop import LoopSocket from tests.loop import LoopSocket
from tests.util import ParamikoTest, test_path from tests.util import ParamikoTest, test_path