De-golf some crypto functions.
encrypt_key and decrypt_key were using a feature of sum() where the second argument is a 'start' value and every futher value is += onto that. Since it was a tuple, the final result is a tuple. Replaced it with a tuple comprehension. get_chunks just needed some variable names but I am not sure the purpose of the increasing chunk size during iteration. It's probably a feature of the MEGA webclient.
This commit is contained in:
parent
1a7c3ea781
commit
85d4f9d6a3
1 changed files with 22 additions and 18 deletions
|
@ -48,8 +48,8 @@ def stringhash(str, aeskey):
|
||||||
"""
|
"""
|
||||||
s32 = str_to_a32(str)
|
s32 = str_to_a32(str)
|
||||||
h32 = [0, 0, 0, 0]
|
h32 = [0, 0, 0, 0]
|
||||||
for i in range(len(s32)):
|
for (index, word) in enumerate(s32):
|
||||||
h32[i % 4] ^= s32[i]
|
h32[index % 4] ^= word
|
||||||
for r in range(0x4000):
|
for r in range(0x4000):
|
||||||
h32 = aes_cbc_encrypt_a32(h32, aeskey)
|
h32 = aes_cbc_encrypt_a32(h32, aeskey)
|
||||||
return a32_to_base64((h32[0], h32[2]))
|
return a32_to_base64((h32[0], h32[2]))
|
||||||
|
@ -68,15 +68,20 @@ def prepare_key(arr):
|
||||||
|
|
||||||
|
|
||||||
def encrypt_key(a, key):
|
def encrypt_key(a, key):
|
||||||
return sum(
|
encrypted = tuple(
|
||||||
(aes_cbc_encrypt_a32(a[i:i + 4], key) for i in range(0, len(a), 4)), ()
|
piece
|
||||||
|
for i in range(0, len(a), 4)
|
||||||
|
for piece in aes_cbc_encrypt_a32(a[i:i + 4], key)
|
||||||
)
|
)
|
||||||
|
return encrypted
|
||||||
|
|
||||||
def decrypt_key(a, key):
|
def decrypt_key(a, key):
|
||||||
return sum(
|
decrypted = tuple(
|
||||||
(aes_cbc_decrypt_a32(a[i:i + 4], key) for i in range(0, len(a), 4)), ()
|
piece
|
||||||
|
for i in range(0, len(a), 4)
|
||||||
|
for piece in aes_cbc_decrypt_a32(a[i:i + 4], key)
|
||||||
)
|
)
|
||||||
|
return decrypted
|
||||||
|
|
||||||
|
|
||||||
def encrypt_attr(attr, key):
|
def encrypt_attr(attr, key):
|
||||||
|
@ -160,19 +165,18 @@ def get_chunks(size):
|
||||||
Given the size of a file in bytes, return tuples (chunk_start, chunk_size)
|
Given the size of a file in bytes, return tuples (chunk_start, chunk_size)
|
||||||
for the purposes of downloading or uploading a file in chunks.
|
for the purposes of downloading or uploading a file in chunks.
|
||||||
"""
|
"""
|
||||||
p = 0
|
chunk_start = 0
|
||||||
s = 0x20000
|
chunk_size = 0x20000
|
||||||
while p + s < size:
|
while chunk_start + chunk_size < size:
|
||||||
yield (p, s)
|
yield (chunk_start, chunk_size)
|
||||||
p += s
|
chunk_start += chunk_size
|
||||||
if s < 0x100000:
|
# why?
|
||||||
s += 0x20000
|
if chunk_size < 0x100000:
|
||||||
yield (p, size - p)
|
chunk_size += 0x20000
|
||||||
|
yield (chunk_start, size - chunk_start)
|
||||||
|
|
||||||
|
|
||||||
def make_id(length):
|
def make_id(length):
|
||||||
text = ''
|
|
||||||
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
for i in range(length):
|
text = ''.join(random.choice(possible) for i in range(length))
|
||||||
text += random.choice(possible)
|
|
||||||
return text
|
return text
|
||||||
|
|
Loading…
Reference in a new issue