Skip to content

Commit

Permalink
perf: helpers.crypt func: base64_url_decode use regex instead of re…
Browse files Browse the repository at this point in the history
…place in a loop
  • Loading branch information
Itz-fork committed Dec 28, 2023
1 parent ee33a7f commit ac1d837
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions megadl/helpers/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Ported from: https://github.com/Itz-fork/pyro-mega.py/blob/master/src/mega/crypto.py

import re
import json
import base64
import struct
Expand All @@ -29,7 +30,6 @@ def str_to_a32(b):
if isinstance(b, str):
b = makebyte(b)
if len(b) % 4:
# pad to multiple of 4
b += b"\0" * (4 - len(b) % 4)
return struct.unpack(">%dI" % (len(b) / 4), b)

Expand All @@ -40,8 +40,7 @@ def base64_to_a32(s):

def base64_url_decode(data):
data += "=="[(2 - len(data) * 3) % 4 :]
for search, replace in (("-", "+"), ("_", "/"), (",", "")):
data = data.replace(search, replace)
data = re.sub(r'[-_,]', lambda x: {'-': '+', '_': '/', ',': ''}[x.group()], data)
return base64.b64decode(data)


Expand Down

0 comments on commit ac1d837

Please sign in to comment.