Python 3 bugfix to `upload` method.

This commit is contained in:
Richard O'Dwyer 2019-10-17 21:02:52 +01:00
parent dcd23d4dd5
commit 4e5f88ef5f
2 changed files with 26 additions and 12 deletions

View file

@ -6,7 +6,7 @@ Release History
0.9.20 (unreleased) 0.9.20 (unreleased)
+++++++++++++++++++ +++++++++++++++++++
- Nothing changed yet. - Python 3 bugfix to ``upload`` method.
0.9.19 (2019-10-16) 0.9.19 (2019-10-16)

View file

@ -4,6 +4,7 @@ from Crypto.Cipher import AES
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from Crypto.Util import Counter from Crypto.Util import Counter
import os import os
import sys
import random import random
import binascii import binascii
import requests import requests
@ -12,10 +13,12 @@ from .errors import ValidationError, RequestError
from .crypto import ( from .crypto import (
a32_to_base64, encrypt_key, base64_url_encode, encrypt_attr, base64_to_a32, a32_to_base64, encrypt_key, base64_url_encode, encrypt_attr, base64_to_a32,
base64_url_decode, decrypt_attr, a32_to_str, get_chunks, str_to_a32, base64_url_decode, decrypt_attr, a32_to_str, get_chunks, str_to_a32,
decrypt_key, mpi_to_int, stringhash, prepare_key, make_id, decrypt_key, mpi_to_int, stringhash, prepare_key, make_id, makebyte
) )
import tempfile import tempfile
PYTHON2 = sys.version_info < (3, )
class Mega(object): class Mega(object):
def __init__(self, options=None): def __init__(self, options=None):
@ -90,11 +93,15 @@ class Mega(object):
private_key = a32_to_str(rsa_private_key) private_key = a32_to_str(rsa_private_key)
self.rsa_private_key = [0, 0, 0, 0] self.rsa_private_key = [0, 0, 0, 0]
for i in range(4): for i in range(4):
if PYTHON2:
l = ( l = (
(ord(private_key[0]) * 256 + ord(private_key[1]) + 7) / 8 (ord(private_key[0]) * 256 + ord(private_key[1]) + 7) / 8
) + 2 ) + 2
else:
l = int(
((private_key[0]) * 256 + (private_key[1]) + 7) / 8
) + 2
self.rsa_private_key[i] = mpi_to_int(private_key[:l]) self.rsa_private_key[i] = mpi_to_int(private_key[:l])
private_key = private_key[l:] private_key = private_key[l:]
@ -671,11 +678,18 @@ class Mega(object):
block = chunk[i:i + 16] block = chunk[i:i + 16]
if len(block) % 16: if len(block) % 16:
block += '\0' * (16 - len(block) % 16) block += makebyte('\0' * (16 - len(block) % 16))
mac_str = mac_encryptor.encrypt(encryptor.encrypt(block)) mac_str = mac_encryptor.encrypt(encryptor.encrypt(block))
# encrypt file and upload # encrypt file and upload
chunk = aes.encrypt(chunk) chunk = aes.encrypt(chunk)
try:
output_file = requests.post(
ul_url + "/" + str(chunk_start),
data=chunk,
timeout=self.timeout
)
except:
output_file = requests.post( output_file = requests.post(
ul_url + "/" + str(chunk_start), ul_url + "/" + str(chunk_start),
data=chunk, data=chunk,