From a925a9d5e57fad4647f1169229fb0789a1700f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 13 Mar 2022 12:39:20 +0100 Subject: Fix #133: Remove rsa/_compat.py There were very few functions in there, and none of them were actually used by the RSA library (just by the test code). --- rsa/_compat.py | 48 -------------------------------- tests/test_common.py | 11 -------- tests/test_compat.py | 78 ---------------------------------------------------- tests/test_pkcs1.py | 3 +- 4 files changed, 1 insertion(+), 139 deletions(-) delete mode 100644 rsa/_compat.py delete mode 100644 tests/test_compat.py diff --git a/rsa/_compat.py b/rsa/_compat.py deleted file mode 100644 index 050e81b..0000000 --- a/rsa/_compat.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright 2011 Sybren A. Stüvel -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Python compatibility wrappers.""" - -from struct import pack - - -def byte(num: int) -> bytes: - """ - Converts a number between 0 and 255 (both inclusive) to a base-256 (byte) - representation. - - :param num: - An unsigned integer between 0 and 255 (both inclusive). - :returns: - A single byte. - """ - return pack("B", num) - - -def xor_bytes(b1: bytes, b2: bytes) -> bytes: - """ - Returns the bitwise XOR result between two bytes objects, b1 ^ b2. - - Bitwise XOR operation is commutative, so order of parameters doesn't - generate different results. If parameters have different length, extra - length of the largest one is ignored. - - :param b1: - First bytes object. - :param b2: - Second bytes object. - :returns: - Bytes object, result of XOR operation. - """ - return bytes(x ^ y for x, y in zip(b1, b2)) diff --git a/tests/test_common.py b/tests/test_common.py index ac56bcd..c6a60d5 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -15,20 +15,9 @@ import unittest import struct -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") - - def test_struct_error_when_out_of_bounds(self): - self.assertRaises(struct.error, byte, 256) - self.assertRaises(struct.error, byte, -1) - - class TestByteSize(unittest.TestCase): def test_values(self): self.assertEqual(byte_size(1 << 1023), 128) diff --git a/tests/test_compat.py b/tests/test_compat.py deleted file mode 100644 index 9c65315..0000000 --- a/tests/test_compat.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright 2011 Sybren A. Stüvel -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest -import struct - -from rsa._compat import byte, xor_bytes - - -class TestByte(unittest.TestCase): - """Tests for single bytes.""" - - def test_byte(self): - for i in range(256): - byt = byte(i) - self.assertIsInstance(byt, bytes) - self.assertEqual(ord(byt), i) - - def test_raises_StructError_on_overflow(self): - self.assertRaises(struct.error, byte, 256) - self.assertRaises(struct.error, byte, -1) - - def test_byte_literal(self): - self.assertIsInstance(b"abc", bytes) - - -class TestBytes(unittest.TestCase): - """Tests for bytes objects.""" - - def setUp(self): - self.b1 = b"\xff\xff\xff\xff" - self.b2 = b"\x00\x00\x00\x00" - self.b3 = b"\xf0\xf0\xf0\xf0" - self.b4 = b"\x4d\x23\xca\xe2" - self.b5 = b"\x9b\x61\x3b\xdc" - self.b6 = b"\xff\xff" - - self.byte_strings = (self.b1, self.b2, self.b3, self.b4, self.b5, self.b6) - - def test_xor_bytes(self): - self.assertEqual(xor_bytes(self.b1, self.b2), b"\xff\xff\xff\xff") - self.assertEqual(xor_bytes(self.b1, self.b3), b"\x0f\x0f\x0f\x0f") - self.assertEqual(xor_bytes(self.b1, self.b4), b"\xb2\xdc\x35\x1d") - self.assertEqual(xor_bytes(self.b1, self.b5), b"\x64\x9e\xc4\x23") - self.assertEqual(xor_bytes(self.b2, self.b3), b"\xf0\xf0\xf0\xf0") - self.assertEqual(xor_bytes(self.b2, self.b4), b"\x4d\x23\xca\xe2") - self.assertEqual(xor_bytes(self.b2, self.b5), b"\x9b\x61\x3b\xdc") - self.assertEqual(xor_bytes(self.b3, self.b4), b"\xbd\xd3\x3a\x12") - self.assertEqual(xor_bytes(self.b3, self.b5), b"\x6b\x91\xcb\x2c") - self.assertEqual(xor_bytes(self.b4, self.b5), b"\xd6\x42\xf1\x3e") - - def test_xor_bytes_length(self): - self.assertEqual(xor_bytes(self.b1, self.b6), b"\x00\x00") - self.assertEqual(xor_bytes(self.b2, self.b6), b"\xff\xff") - self.assertEqual(xor_bytes(self.b3, self.b6), b"\x0f\x0f") - self.assertEqual(xor_bytes(self.b4, self.b6), b"\xb2\xdc") - self.assertEqual(xor_bytes(self.b5, self.b6), b"\x64\x9e") - self.assertEqual(xor_bytes(self.b6, b""), b"") - - def test_xor_bytes_commutative(self): - for first in self.byte_strings: - for second in self.byte_strings: - min_length = min(len(first), len(second)) - result = xor_bytes(first, second) - - self.assertEqual(result, xor_bytes(second, first)) - self.assertEqual(len(result), min_length) diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index f2d4957..6b14d8f 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -20,7 +20,6 @@ import unittest import rsa from rsa import pkcs1 -from rsa._compat import byte class BinaryTest(unittest.TestCase): @@ -48,7 +47,7 @@ class BinaryTest(unittest.TestCase): self.assertIsInstance(a, int) altered_a = (a + 1) % 256 - encrypted = encrypted[:5] + byte(altered_a) + encrypted[6:] + encrypted = encrypted[:5] + bytes([altered_a]) + encrypted[6:] self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted, self.priv) -- cgit v1.2.1