mirror of
https://github.com/zulip/zulip.git
synced 2025-10-29 19:13:53 +00:00
python: Replace binascii with bytes.hex to skip some decode operations.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
aaa7b766d8
commit
18d0e4664c
@@ -1,4 +1,3 @@
|
||||
import binascii
|
||||
import hashlib
|
||||
import 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 proxy
|
||||
|
||||
@@ -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 binascii
|
||||
|
||||
from 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 * 2
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import base64
|
||||
import binascii
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
@@ -45,10 +44,10 @@ DeviceToken = Union[PushDeviceToken, "RemotePushDeviceToken"]
|
||||
|
||||
# We store the token as b64, but apns-client wants hex strings
|
||||
def 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 iOS
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import binascii
|
||||
|
||||
from django.conf import settings
|
||||
from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, HttpResponseNotFound
|
||||
from 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:
|
||||
|
||||
Reference in New Issue
Block a user