aboutsummaryrefslogtreecommitdiff
path: root/rsa/_compat.py
diff options
context:
space:
mode:
Diffstat (limited to 'rsa/_compat.py')
-rw-r--r--rsa/_compat.py118
1 files changed, 2 insertions, 116 deletions
diff --git a/rsa/_compat.py b/rsa/_compat.py
index 71197a5..050e81b 100644
--- a/rsa/_compat.py
+++ b/rsa/_compat.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-#
# Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,84 +14,14 @@
"""Python compatibility wrappers."""
-from __future__ import absolute_import
-
-import itertools
-import sys
from struct import pack
-MAX_INT = sys.maxsize
-MAX_INT64 = (1 << 63) - 1
-MAX_INT32 = (1 << 31) - 1
-MAX_INT16 = (1 << 15) - 1
-
-PY2 = sys.version_info[0] == 2
-
-# Determine the word size of the processor.
-if MAX_INT == MAX_INT64:
- # 64-bit processor.
- MACHINE_WORD_SIZE = 64
-elif MAX_INT == MAX_INT32:
- # 32-bit processor.
- MACHINE_WORD_SIZE = 32
-else:
- # Else we just assume 64-bit processor keeping up with modern times.
- MACHINE_WORD_SIZE = 64
-
-if PY2:
- integer_types = (int, long)
- range = xrange
- zip = itertools.izip
-else:
- integer_types = (int, )
- range = range
- zip = zip
-
-def write_to_stdout(data):
- """Writes bytes to stdout
-
- :type data: bytes
- """
- if PY2:
- sys.stdout.write(data)
- else:
- # On Py3 we must use the buffer interface to write bytes.
- sys.stdout.buffer.write(data)
-
-
-def is_bytes(obj):
- """
- Determines whether the given value is a byte string.
-
- :param obj:
- The value to test.
- :returns:
- ``True`` if ``value`` is a byte string; ``False`` otherwise.
- """
- return isinstance(obj, bytes)
-
-
-def is_integer(obj):
- """
- Determines whether the given value is an integer.
-
- :param obj:
- The value to test.
- :returns:
- ``True`` if ``value`` is an integer; ``False`` otherwise.
- """
- return isinstance(obj, integer_types)
-
-
-def byte(num):
+def byte(num: int) -> bytes:
"""
Converts a number between 0 and 255 (both inclusive) to a base-256 (byte)
representation.
- Use it as a replacement for ``chr`` where you are expecting a byte
- because this will work on all current versions of Python::
-
:param num:
An unsigned integer between 0 and 255 (both inclusive).
:returns:
@@ -102,7 +30,7 @@ def byte(num):
return pack("B", num)
-def xor_bytes(b1, b2):
+def xor_bytes(b1: bytes, b2: bytes) -> bytes:
"""
Returns the bitwise XOR result between two bytes objects, b1 ^ b2.
@@ -117,46 +45,4 @@ def xor_bytes(b1, b2):
:returns:
Bytes object, result of XOR operation.
"""
- if PY2:
- return ''.join(byte(ord(x) ^ ord(y)) for x, y in zip(b1, b2))
-
return bytes(x ^ y for x, y in zip(b1, b2))
-
-
-def get_word_alignment(num, force_arch=64,
- _machine_word_size=MACHINE_WORD_SIZE):
- """
- Returns alignment details for the given number based on the platform
- Python is running on.
-
- :param num:
- Unsigned integral number.
- :param force_arch:
- If you don't want to use 64-bit unsigned chunks, set this to
- anything other than 64. 32-bit chunks will be preferred then.
- Default 64 will be used when on a 64-bit machine.
- :param _machine_word_size:
- (Internal) The machine word size used for alignment.
- :returns:
- 4-tuple::
-
- (word_bits, word_bytes,
- max_uint, packing_format_type)
- """
- max_uint64 = 0xffffffffffffffff
- max_uint32 = 0xffffffff
- max_uint16 = 0xffff
- max_uint8 = 0xff
-
- if force_arch == 64 and _machine_word_size >= 64 and num > max_uint32:
- # 64-bit unsigned integer.
- return 64, 8, max_uint64, "Q"
- elif num > max_uint16:
- # 32-bit unsigned integer
- return 32, 4, max_uint32, "L"
- elif num > max_uint8:
- # 16-bit unsigned integer.
- return 16, 2, max_uint16, "H"
- else:
- # 8-bit unsigned integer.
- return 8, 1, max_uint8, "B"