summaryrefslogtreecommitdiff
path: root/rsa/pkcs1.py
diff options
context:
space:
mode:
authorYesudeep Mangalapilly <yesudeep@gmail.com>2011-08-11 02:38:13 +0530
committerYesudeep Mangalapilly <yesudeep@gmail.com>2011-08-11 02:38:13 +0530
commit2eeea2ca19fdea9f354bbec685e168fee370d19f (patch)
treed780c2c3d49075c3c26ce587c17e5b1c9921f5b0 /rsa/pkcs1.py
parent0a0116f248a79b404be02689498889e21b0b7dde (diff)
downloadrsa-2eeea2ca19fdea9f354bbec685e168fee370d19f.tar.gz
Porting to Python 3 complete. All tests except pyasn1 stuff pass.
Diffstat (limited to 'rsa/pkcs1.py')
-rw-r--r--rsa/pkcs1.py35
1 files changed, 18 insertions, 17 deletions
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