Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
python: Replace binascii with bytes.hex to skip some decode operations.
Anders Kaseorg18d0e4664cd7
diff --git a/zerver/lib/camo.py b/zerver/lib/camo.pyindex 94e0ee1aad4..35af326ccfd 100644--- a/zerver/lib/camo.py+++ b/zerver/lib/camo.py@@ -1,4 +1,3 @@-import binasciiimport hashlibimport hmac@@ -9,8 +8,7 @@ def generate_camo_url(url: str) -> str:encoded_url = url.encode("utf-8")encoded_camo_key = settings.CAMO_KEY.encode("utf-8")digest = hmac.new(encoded_camo_key, encoded_url, hashlib.sha1).hexdigest()- hex_encoded_url = binascii.b2a_hex(encoded_url)- return "{}/{}".format(digest, hex_encoded_url.decode("utf-8"))+ return "{}/{}".format(digest, encoded_url.hex())# Encodes the provided URL using the same algorithm used by the camo# caching https image proxydiff --git a/zerver/lib/mobile_auth_otp.py b/zerver/lib/mobile_auth_otp.pyindex 14c3c006ebc..a2907062a84 100644--- a/zerver/lib/mobile_auth_otp.py+++ b/zerver/lib/mobile_auth_otp.py@@ -7,7 +7,6 @@## The decryption logic here isn't actually used by the flow; we just# have it here as part of testing the overall library.-import binasciifrom zerver.models import UserProfile@@ -21,11 +20,11 @@ def xor_hex_strings(bytes_a: str, bytes_b: str) -> str:def ascii_to_hex(input_string: str) -> str:"""Given an ascii string, encode it as a hex string"""- return "".join(hex(ord(c))[2:].zfill(2) for c in input_string)+ return input_string.encode().hex()def hex_to_ascii(input_string: str) -> str:"""Given a hex array, decode it back to a string"""- return binascii.unhexlify(input_string).decode('utf8')+ return bytes.fromhex(input_string).decode()def otp_encrypt_api_key(api_key: str, otp: str) -> str:assert len(otp) == UserProfile.API_KEY_LENGTH * 2diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.pyindex 1c2faa4b61c..ce38ec59aa6 100644--- a/zerver/lib/push_notifications.py+++ b/zerver/lib/push_notifications.py@@ -1,5 +1,4 @@import base64-import binasciiimport loggingimport reimport time@@ -45,10 +44,10 @@ DeviceToken = Union[PushDeviceToken, "RemotePushDeviceToken"]# We store the token as b64, but apns-client wants hex stringsdef b64_to_hex(data: str) -> str:- return binascii.hexlify(base64.b64decode(data)).decode('utf-8')+ return base64.b64decode(data).hex()def hex_to_b64(data: str) -> str:- return base64.b64encode(binascii.unhexlify(data)).decode()+ return base64.b64encode(bytes.fromhex(data)).decode()## Sending to APNs, for iOSdiff --git a/zerver/views/camo.py b/zerver/views/camo.pyindex 414eb6f6020..3f3991adf7e 100644--- a/zerver/views/camo.py+++ b/zerver/views/camo.py@@ -1,5 +1,3 @@-import binascii-from django.conf import settingsfrom django.http import HttpRequest, HttpResponse, HttpResponseForbidden, HttpResponseNotFoundfrom django.shortcuts import redirect@@ -14,9 +12,7 @@ def handle_camo_url(request: HttpRequest, digest: str,if not settings.THUMBOR_SERVES_CAMO:return HttpResponseNotFound()- hex_encoded_url = received_url.encode('utf-8')- hex_decoded_url = binascii.a2b_hex(hex_encoded_url)- original_url = hex_decoded_url.decode('utf-8')+ original_url = bytes.fromhex(received_url).decode()if is_camo_url_valid(digest, original_url):return redirect(generate_thumbnail_url(original_url, is_camo_url=True))else: