Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions mega/crypto.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand Down
20 changes: 10 additions & 10 deletions mega/mega.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -88,15 +88,15 @@ 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:]

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]))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
8 changes: 8 additions & 0 deletions simple-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from mega import Mega

email = "[email protected]"
password = "Er0senn1n"

m = Mega.from_credentials(email, password)

m.download_from_url('https://mega.co.nz/#!wYo3AYZC!Zwi1f3ANtYwKNOc07fwuN1enOoRj4CreFouuGqi4D6Y')
2 changes: 0 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?")
Expand Down