From e97d0d730367ad3b49efc712c01509500939c904 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 02:38:13 +0530 Subject: Porting to Python 3 complete. All tests except pyasn1 stuff pass. --- rsa/pkcs1.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'rsa/pkcs1.py') diff --git a/rsa/pkcs1.py b/rsa/pkcs1.py index 9686cf9..8c6d290 100644 --- a/rsa/pkcs1.py +++ b/rsa/pkcs1.py @@ -31,15 +31,16 @@ SUCH INFORMATION to your users. import hashlib import os +from rsa._compat import b from rsa import common, transform, core, varblock # ASN.1 codes that describe the hash algorithm used. HASH_ASN1 = { - 'MD5': '\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10', - 'SHA-1': '\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14', - 'SHA-256': '\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20', - 'SHA-384': '\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30', - 'SHA-512': '\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40', + 'MD5': b('\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10'), + 'SHA-1': b('\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14'), + 'SHA-256': b('\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20'), + 'SHA-384': b('\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30'), + 'SHA-512': b('\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40'), } HASH_METHODS = { @@ -82,7 +83,7 @@ def _pad_for_encryption(message, target_length): ' space for %i' % (msglength, max_msglength)) # Get random padding - padding = '' + padding = b('') padding_length = target_length - msglength - 3 # We remove 0-bytes, so we'll end up with less padding than we've asked for, @@ -94,14 +95,14 @@ def _pad_for_encryption(message, target_length): # after removing the 0-bytes. This increases the chance of getting # enough bytes, especially when needed_bytes is small new_padding = os.urandom(needed_bytes + 5) - new_padding = new_padding.replace('\x00', '') + new_padding = new_padding.replace(b('\x00'), b('')) padding = padding + new_padding[:needed_bytes] assert len(padding) == padding_length - return ''.join(['\x00\x02', + return b('').join([b('\x00\x02'), padding, - '\x00', + b('\x00'), message]) @@ -133,9 +134,9 @@ def _pad_for_signing(message, target_length): padding_length = target_length - msglength - 3 - return ''.join(['\x00\x01', - padding_length * '\xff', - '\x00', + return b('').join([b('\x00\x01'), + padding_length * b('\xff'), + b('\x00'), message]) @@ -227,12 +228,12 @@ def decrypt(crypto, priv_key): cleartext = transform.int2bytes(decrypted, blocksize) # If we can't find the cleartext marker, decryption failed. - if cleartext[0:2] != '\x00\x02': + if cleartext[0:2] != b('\x00\x02'): raise DecryptionError('Decryption failed') # Find the 00 separator between the padding and the message try: - sep_idx = cleartext.index('\x00', 2) + sep_idx = cleartext.index(b('\x00'), 2) except ValueError: raise DecryptionError('Decryption failed') @@ -303,12 +304,12 @@ def verify(message, signature, pub_key): clearsig = transform.int2bytes(decrypted, blocksize) # If we can't find the signature marker, verification failed. - if clearsig[0:2] != '\x00\x01': + if clearsig[0:2] != b('\x00\x01'): raise VerificationError('Verification failed') # Find the 00 separator between the padding and the payload try: - sep_idx = clearsig.index('\x00', 2) + sep_idx = clearsig.index(b('\x00'), 2) except ValueError: raise VerificationError('Verification failed') @@ -361,7 +362,7 @@ def _find_method_hash(method_hash): ''' - for (hashname, asn1code) in HASH_ASN1.iteritems(): + for (hashname, asn1code) in HASH_ASN1.items(): if not method_hash.startswith(asn1code): continue -- cgit v1.2.1