From 82988c77fbbc893e5659e3085bbc32f0580c74d1 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Tue, 2 Apr 2019 02:50:17 +0000 Subject: Drop C blowfish cryptor --- pandora/transport.py | 46 ++---------------------------------- setup.py | 39 +++++++++++------------------- tests/test_pandora/test_transport.py | 31 ------------------------ 3 files changed, 16 insertions(+), 100 deletions(-) diff --git a/pandora/transport.py b/pandora/transport.py index 0174eac..6f1a174 100644 --- a/pandora/transport.py +++ b/pandora/transport.py @@ -13,16 +13,12 @@ import random import time import json import base64 +import blowfish import requests from requests.adapters import HTTPAdapter from .errors import PandoraException -try: - import blowfish -except ImportError: - blowfish = None - DEFAULT_API_HOST = "tuner.pandora.com/services/json/" @@ -272,40 +268,6 @@ class BlowfishCryptor(object): return data[:-pad_size] -class CryptographyBlowfish(BlowfishCryptor): - """Cryptography Blowfish Cryptor - - Uses the python cryptography library which wraps OpenSSL. Compatible with - both Python 2 and 3 but requires a native dependency. - """ - - def __init__(self, key): - from cryptography.hazmat.backends import default_backend - from cryptography.hazmat.primitives.ciphers import Cipher - from cryptography.hazmat.primitives.ciphers.modes import ECB - from cryptography.hazmat.primitives.ciphers.algorithms import Blowfish - - self.cipher = Cipher( - Blowfish(key.encode("ascii")), ECB(), backend=default_backend()) - - def _make_bytearray(self, data): - return bytearray(len(data) + (self.block_size - 1)) - - def decrypt(self, data, strip_padding=True): - buf = self._make_bytearray(data) - dec = self.cipher.decryptor() - len_dec = dec.update_into(data, buf) - data = bytes(buf[:len_dec]) + dec.finalize() - return self._strip_padding(data) if strip_padding else data - - def encrypt(self, data): - data = self._add_padding(data) - enc = self.cipher.encryptor() - buf = self._make_bytearray(data) - len_enc = enc.update_into(data, buf) - return bytes(buf[:len_enc]) + enc.finalize() - - class PurePythonBlowfish(BlowfishCryptor): """Pure Python 3 Blowfish Cryptor @@ -323,10 +285,6 @@ class PurePythonBlowfish(BlowfishCryptor): return b"".join(self.cipher.encrypt_ecb(self._add_padding(data))) -# Python 3 users can use pure-python mode, if possible prefer that as default -_default_crypto = PurePythonBlowfish if blowfish else CryptographyBlowfish - - class Encryptor(object): """Pandora Blowfish Encryptor @@ -334,7 +292,7 @@ class Encryptor(object): API request and response. It handles the formats that the API expects. """ - def __init__(self, in_key, out_key, crypto_class=_default_crypto): + def __init__(self, in_key, out_key, crypto_class=PurePythonBlowfish): self.bf_out = crypto_class(out_key) self.bf_in = crypto_class(in_key) diff --git a/setup.py b/setup.py index 62aec94..0e4c5fb 100755 --- a/setup.py +++ b/setup.py @@ -37,29 +37,6 @@ class TestsWithCoverage(test, object): cov.html_report() -requires = { - "setup_requires": [ - "wheel", - "flake8>=3.3", - ], - "tests_require": [ - "mock>=2,<3", - "coverage>=4.1,<5", - "cryptography>=2,<3", - ], - "install_requires": [ - "requests>=2,<3", - ], -} - - -if sys.version_info.major == 3 and sys.version_info.minor >= 4: - requires["install_requires"].append("blowfish>=0.6.1,<1.0") -else: - requires["install_requires"].append("cryptography>=2,<3") - requires["install_requires"].append("enum34") - - setup( name="pydora", version="2.0.0", @@ -79,6 +56,19 @@ setup( "pydora-configure = pydora.configure:main", ], }, + setup_requires=[ + "wheel", + "flake8>=3.3", + ], + tests_require=[ + "mock>=2,<3", + "coverage>=4.1,<5", + "cryptography>=2,<3", + ], + install_requires=[ + "requests>=2,<3", + "blowfish>=0.6.1,<1.0", + ], classifiers=[ "Development Status :: 5 - Production/Stable", "Environment :: Console", @@ -91,6 +81,5 @@ setup( "Programming Language :: Python :: 3.7", "Topic :: Internet :: WWW/HTTP", "Topic :: Multimedia :: Sound/Audio :: Players", - ], - **requires + ] ) diff --git a/tests/test_pandora/test_transport.py b/tests/test_pandora/test_transport.py index 862a12f..96dd40e 100644 --- a/tests/test_pandora/test_transport.py +++ b/tests/test_pandora/test_transport.py @@ -281,26 +281,6 @@ class TestPurePythonBlowfishCryptor(TestCase, CommonCryptorTestCases): self.cryptor.cipher = self.cipher -class TestCryptographyBlowfish(TestCase, CommonCryptorTestCases): - - class FakeCipher(object): - - def update_into(self, val, buf): - for i, v in enumerate(val): - buf[i] = v - return len(val) - - def finalize(self): - return b"" - - def setUp(self): - self.cipher = Mock() - self.cipher.encryptor.return_value = self.FakeCipher() - self.cipher.decryptor.return_value = self.FakeCipher() - self.cryptor = t.CryptographyBlowfish("keys") - self.cryptor.cipher = self.cipher - - class TestEncryptor(TestCase): ENCODED_JSON = "7b22666f6f223a22626172227d" @@ -335,14 +315,3 @@ class TestEncryptor(TestCase): self.assertEqual( self.EXPECTED_TIME, self.cryptor.decrypt_sync_time(self.ENCODED_TIME)) - - -class TestDefaultStrategy(TestCase): - - def test_blowfish_not_available(self): - del sys.modules["pandora.transport"] - sys.modules["blowfish"] = None - - import pandora.transport as t - self.assertIsNone(t.blowfish) - self.assertIs(t._default_crypto, t.CryptographyBlowfish) -- cgit v1.2.3