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
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'

View File

@ -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:

View File

@ -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

View File

@ -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)

View File

@ -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()

View File

@ -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())

View File

@ -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):

View File

@ -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