diff --git a/mega/crypto.py b/mega/crypto.py old mode 100644 new mode 100755 index 8e62705..503768b --- a/mega/crypto.py +++ b/mega/crypto.py @@ -3,7 +3,7 @@ from Crypto.Cipher import AES -from .utils import a32_to_str, str_to_a32, a32_to_base64 +from utils import a32_to_str, str_to_a32, a32_to_base64 def aes_cbc_encrypt(data, key): @@ -27,19 +27,19 @@ def aes_cbc_decrypt_a32(data, key): def stringhash(s, aeskey): s32 = str_to_a32(s) h32 = [0, 0, 0, 0] - for i in xrange(len(s32)): + for i in range(len(s32)): h32[i % 4] ^= s32[i] - for _ in xrange(0x4000): + for _ in range(0x4000): h32 = aes_cbc_encrypt_a32(h32, aeskey) return a32_to_base64((h32[0], h32[2])) def prepare_key(a): pkey = [0x93C467E3, 0x7DB0C7A4, 0xD1BE3F81, 0x0152CB56] - for _ in xrange(0x10000): - for j in xrange(0, len(a), 4): + for _ in range(0x10000): + for j in range(0, len(a), 4): key = [0, 0, 0, 0] - for i in xrange(4): + for i in range(4): if i + j < len(a): key[i] = a[i + j] pkey = aes_cbc_encrypt_a32(pkey, key) @@ -49,13 +49,13 @@ def prepare_key(a): def encrypt_key(a, key): return sum( (aes_cbc_encrypt_a32(a[i:i+4], key) - for i in xrange(0, len(a), 4)), ()) + for i in range(0, len(a), 4)), ()) def decrypt_key(a, key): return sum( (aes_cbc_decrypt_a32(a[i:i+4], key) - for i in xrange(0, len(a), 4)), ()) + for i in range(0, len(a), 4)), ()) def enc_attr(attr, key): diff --git a/mega/mega.py b/mega/mega.py old mode 100644 new mode 100755 index 3507e75..1a1a2da --- a/mega/mega.py +++ b/mega/mega.py @@ -10,11 +10,11 @@ from Crypto.PublicKey import RSA from Crypto.Util import Counter -from .crypto import prepare_key, stringhash, encrypt_key, decrypt_key,\ +from crypto import prepare_key, stringhash, encrypt_key, decrypt_key,\ enc_attr, dec_attr, aes_cbc_encrypt_a32 -from .utils import a32_to_str, str_to_a32, a32_to_base64, base64_to_a32,\ +from utils import a32_to_str, str_to_a32, a32_to_base64, base64_to_a32,\ mpi2int, base64urlencode, base64urldecode, get_chunks -from .exceptions import MegaRequestException, MegaIncorrectPasswordExcetion +from exceptions import MegaRequestException, MegaIncorrectPasswordExcetion class Mega(object): @@ -88,7 +88,7 @@ def _login_common(self, res, password): privk = a32_to_str(rsa_priv_key) self.rsa_priv_key = [0, 0, 0, 0] - for i in xrange(4): + for i in range(4): l = ((ord(privk[0]) * 256 + ord(privk[1]) + 7) / 8) + 2 self.rsa_priv_key[i] = mpi2int(privk[:l]) privk = privk[l:] @@ -96,7 +96,7 @@ def _login_common(self, res, password): enc_sid = mpi2int(base64urldecode(res['csid'])) decrypter = RSA.construct( (self.rsa_priv_key[0] * self.rsa_priv_key[1], - 0L, + 0, self.rsa_priv_key[2], self.rsa_priv_key[0], self.rsa_priv_key[1])) @@ -173,7 +173,7 @@ def download_file(self, file_id, file_key, public=False): outfile.write(chunk) chunk_mac = [iv[0], iv[1], iv[0], iv[1]] - for i in xrange(0, len(chunk), 16): + for i in range(0, len(chunk), 16): block = chunk[i:i+16] if len(block) % 16: block += '\0' * (16 - (len(block) % 16)) @@ -205,15 +205,15 @@ def get_public_url(self, file_id, file_key): def uploadfile(self, filename, dst=None): if not dst: - root_id = getattr(self, 'root_id') - if not root_id: + root_id = getattr(self, 'root_id', None) + if root_id == None: self.get_files() dst = self.root_id infile = open(filename, 'rb') size = os.path.getsize(filename) ul_url = self.api_req({'a': 'u', 's': size})['p'] - ul_key = [random.randint(0, 0xFFFFFFFF) for _ in xrange(6)] + ul_key = [random.randint(0, 0xFFFFFFFF) for _ in range(6)] counter = Counter.new( 128, initial_value=((ul_key[4] << 32) + ul_key[5]) << 64) encryptor = AES.new( @@ -226,7 +226,7 @@ def uploadfile(self, filename, dst=None): chunk = infile.read(chunk_size) chunk_mac = [ul_key[4], ul_key[5], ul_key[4], ul_key[5]] - for i in xrange(0, len(chunk), 16): + for i in range(0, len(chunk), 16): block = chunk[i:i+16] if len(block) % 16: block += '\0' * (16 - len(block) % 16) diff --git a/setup.py b/setup.py index e0913bd..90ed028 100644 --- a/setup.py +++ b/setup.py @@ -48,9 +48,9 @@ def get_package_data(package): if sys.argv[-1] == 'publish': os.system("python setup.py sdist upload") args = {'version': version} - print "You probably want to also tag the version now:" - print " git tag -a %(version)s -m 'version %(version)s'" % args - print " git push --tags" + print("You probably want to also tag the version now:") + print(" git tag -a %(version)s -m 'version %(version)s'" % args) + print(" git push --tags") sys.exit() diff --git a/simple-test.py b/simple-test.py new file mode 100644 index 0000000..2d1c550 --- /dev/null +++ b/simple-test.py @@ -0,0 +1,8 @@ +from mega import Mega + +email = "juanjosalvador@netc.eu" +password = "Er0senn1n" + +m = Mega.from_credentials(email, password) + +m.download_from_url('https://mega.co.nz/#!wYo3AYZC!Zwi1f3ANtYwKNOc07fwuN1enOoRj4CreFouuGqi4D6Y') diff --git a/tests.py b/tests.py index cebae39..8669c42 100644 --- a/tests.py +++ b/tests.py @@ -22,8 +22,6 @@ def _check_file_exists(self, file_name, files): return uploaded def _test_upload_file(self, api): - api.get_files() - # Create temp file uFile, uFilePath = tempfile.mkstemp() os.write(uFile, "Does it work?")