Skip to content
This repository was archived by the owner on Apr 20, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions rsa/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@
# Else we just assume 64-bit processor keeping up with modern times.
MACHINE_WORD_SIZE = 64

try:
# < Python3
unicode_type = unicode
except NameError:
# Python3.
unicode_type = str

# Fake byte literals.
if str is unicode_type:
def byte_literal(s):
return s.encode('latin1')
else:
def byte_literal(s):
return s

# Range generator.
try:
# < Python3
Expand All @@ -66,12 +51,6 @@ def byte_literal(s):
except NameError:
integer_types = (int,)

b = byte_literal

# To avoid calling b() multiple times in tight loops.
ZERO_BYTE = b('\x00')
EMPTY_BYTE = b('')


def is_bytes(obj):
"""
Expand Down
6 changes: 3 additions & 3 deletions rsa/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import logging

from rsa._compat import b, range
from rsa._compat import range
import rsa.prime
import rsa.pem
import rsa.common
Expand Down Expand Up @@ -563,7 +563,7 @@ def _load_pkcs1_pem(cls, keyfile):
:return: a PrivateKey object
"""

der = rsa.pem.load_pem(keyfile, b('RSA PRIVATE KEY'))
der = rsa.pem.load_pem(keyfile, b'RSA PRIVATE KEY')
return cls._load_pkcs1_der(der)

def _save_pkcs1_pem(self):
Expand All @@ -574,7 +574,7 @@ def _save_pkcs1_pem(self):
"""

der = self._save_pkcs1_der()
return rsa.pem.save_pem(der, b('RSA PRIVATE KEY'))
return rsa.pem.save_pem(der, b'RSA PRIVATE KEY')


def find_p_q(nbits, getprime_func=rsa.prime.getprime, accurate=True):
Expand Down
16 changes: 8 additions & 8 deletions rsa/pem.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import base64

from rsa._compat import b, is_bytes, range
from rsa._compat import is_bytes, range


def _markers(pem_marker):
Expand All @@ -29,8 +29,8 @@ def _markers(pem_marker):
if not is_bytes(pem_marker):
pem_marker = pem_marker.encode('ascii')

return (b('-----BEGIN ') + pem_marker + b('-----'),
b('-----END ') + pem_marker + b('-----'))
return (b'-----BEGIN ' + pem_marker + b'-----',
b'-----END ' + pem_marker + b'-----')


def load_pem(contents, pem_marker):
Expand Down Expand Up @@ -82,7 +82,7 @@ def load_pem(contents, pem_marker):
break

# Load fields
if b(':') in line:
if b':' in line:
continue

pem_lines.append(line)
Expand All @@ -95,7 +95,7 @@ def load_pem(contents, pem_marker):
raise ValueError('No PEM end marker "%s" found' % pem_end)

# Base64-decode the contents
pem = b('').join(pem_lines)
pem = b''.join(pem_lines)
return base64.standard_b64decode(pem)


Expand All @@ -113,14 +113,14 @@ def save_pem(contents, pem_marker):

(pem_start, pem_end) = _markers(pem_marker)

b64 = base64.standard_b64encode(contents).replace(b('\n'), b(''))
b64 = base64.standard_b64encode(contents).replace(b'\n', b'')
pem_lines = [pem_start]

for block_start in range(0, len(b64), 64):
block = b64[block_start:block_start + 64]
pem_lines.append(block)

pem_lines.append(pem_end)
pem_lines.append(b(''))
pem_lines.append(b'')

return b('\n').join(pem_lines)
return b'\n'.join(pem_lines)
36 changes: 18 additions & 18 deletions rsa/pkcs1.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
import hashlib
import os

from rsa._compat import b, range
from rsa._compat import range
from rsa import common, transform, core

# ASN.1 codes that describe the hash algorithm used.
HASH_ASN1 = {
'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'),
'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 = {
Expand Down Expand Up @@ -87,7 +87,7 @@ def _pad_for_encryption(message, target_length):
' space for %i' % (msglength, max_msglength))

# Get random padding
padding = b('')
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,
Expand All @@ -99,15 +99,15 @@ 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(b('\x00'), b(''))
new_padding = new_padding.replace(b'\x00', b'')
padding = padding + new_padding[:needed_bytes]

assert len(padding) == padding_length

return b('').join([b('\x00\x02'),
padding,
b('\x00'),
message])
return b''.join([b'\x00\x02',
padding,
b'\x00',
message])


def _pad_for_signing(message, target_length):
Expand Down Expand Up @@ -138,10 +138,10 @@ def _pad_for_signing(message, target_length):

padding_length = target_length - msglength - 3

return b('').join([b('\x00\x01'),
padding_length * b('\xff'),
b('\x00'),
message])
return b''.join([b'\x00\x01',
padding_length * b'\xff',
b'\x00',
message])


def encrypt(message, pub_key):
Expand Down Expand Up @@ -233,12 +233,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] != b('\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(b('\x00'), 2)
sep_idx = cleartext.index(b'\x00', 2)
except ValueError:
raise DecryptionError('Decryption failed')

Expand Down
22 changes: 11 additions & 11 deletions rsa/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from struct import pack

from rsa import common
from rsa._compat import is_integer, b, byte, get_word_alignment, ZERO_BYTE, EMPTY_BYTE
from rsa._compat import byte, is_integer, get_word_alignment


def bytes2int(raw_bytes):
Expand Down Expand Up @@ -83,7 +83,7 @@ def _int2bytes(number, block_size=None):
# Do some bounds checking
if number == 0:
needed_bytes = 1
raw_bytes = [ZERO_BYTE]
raw_bytes = [b'\x00']
else:
needed_bytes = common.byte_size(number)
raw_bytes = []
Expand All @@ -101,14 +101,14 @@ def _int2bytes(number, block_size=None):

# Pad with zeroes to fill the block
if block_size and block_size > 0:
padding = (block_size - needed_bytes) * ZERO_BYTE
padding = (block_size - needed_bytes) * b'\x00'
else:
padding = EMPTY_BYTE
padding = b''

return padding + EMPTY_BYTE.join(raw_bytes)
return padding + b''.join(raw_bytes)


def bytes_leading(raw_bytes, needle=ZERO_BYTE):
def bytes_leading(raw_bytes, needle=b'\x00'):
"""
Finds the number of prefixed byte occurrences in the haystack.

Expand All @@ -117,7 +117,7 @@ def bytes_leading(raw_bytes, needle=ZERO_BYTE):
:param raw_bytes:
Raw bytes.
:param needle:
The byte to count. Default \000.
The byte to count. Default \x00.
:returns:
The number of leading needle bytes.
"""
Expand Down Expand Up @@ -177,7 +177,7 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
# Ensure these are integers.
number & 1

raw_bytes = b('')
raw_bytes = b''

# Pack the integer one machine word at a time into bytes.
num = number
Expand All @@ -189,7 +189,7 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
# Obtain the index of the first non-zero byte.
zero_leading = bytes_leading(raw_bytes)
if number == 0:
raw_bytes = ZERO_BYTE
raw_bytes = b'\x00'
# De-padding.
raw_bytes = raw_bytes[zero_leading:]

Expand All @@ -200,12 +200,12 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
"Need %d bytes for number, but fill size is %d" %
(length, fill_size)
)
raw_bytes = raw_bytes.rjust(fill_size, ZERO_BYTE)
raw_bytes = raw_bytes.rjust(fill_size, b'\x00')
elif chunk_size and chunk_size > 0:
remainder = length % chunk_size
if remainder:
padding_size = chunk_size - remainder
raw_bytes = raw_bytes.rjust(length + padding_size, ZERO_BYTE)
raw_bytes = raw_bytes.rjust(length + padding_size, b'\x00')
return raw_bytes


Expand Down
7 changes: 3 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import rsa
import rsa.cli
import rsa.util
from rsa._compat import b

if sys.version_info[0] < 3:
def make_buffer():
Expand Down Expand Up @@ -135,8 +134,8 @@ def test_keygen_priv_stdout(self):
rsa.cli.keygen()

lines = get_bytes_out(out).splitlines()
self.assertEqual(b('-----BEGIN RSA PRIVATE KEY-----'), lines[0])
self.assertEqual(b('-----END RSA PRIVATE KEY-----'), lines[-1])
self.assertEqual(b'-----BEGIN RSA PRIVATE KEY-----', lines[0])
self.assertEqual(b'-----END RSA PRIVATE KEY-----', lines[-1])

# The key size should be shown on stderr
self.assertTrue('128-bit key' in err.getvalue())
Expand Down Expand Up @@ -217,7 +216,7 @@ def test_encrypt_decrypt(self):

# We should have the original cleartext on stdout now.
output = get_bytes_out(out)
self.assertEqual(b('Hello cleartext RSA users!'), output)
self.assertEqual(b'Hello cleartext RSA users!', output)

@cleanup_files('encrypted.txt', 'cleartext.txt')
def test_encrypt_decrypt_unhappy(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

import unittest
import struct
from rsa._compat import byte, b
from rsa._compat import byte
from rsa.common import byte_size, bit_size, inverse


class TestByte(unittest.TestCase):
def test_values(self):
self.assertEqual(byte(0), b('\x00'))
self.assertEqual(byte(255), b('\xff'))
self.assertEqual(byte(0), b'\x00')
self.assertEqual(byte(255), b'\xff')

def test_struct_error_when_out_of_bounds(self):
self.assertRaises(struct.error, byte, 256)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import struct
import sys

from rsa._compat import b, byte, is_bytes, range
from rsa._compat import byte, is_bytes, range


class TestByte(unittest.TestCase):
Expand All @@ -33,4 +33,4 @@ def test_raises_StructError_on_overflow(self):
self.assertRaises(struct.error, byte, -1)

def test_byte_literal(self):
self.assertIsInstance(b('abc'), bytes)
self.assertIsInstance(b'abc', bytes)
Loading