fixed api resp issue #2, reformatted code pep 8 style
This commit is contained in:
parent
ff91521628
commit
2493a10b88
5 changed files with 66 additions and 32 deletions
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
# Compiled source #
|
||||
###################
|
||||
*.pyc
|
||||
|
||||
# Hidden files #
|
||||
###################
|
||||
.*
|
11
crypto.py
11
crypto.py
|
@ -5,20 +5,25 @@ import struct
|
|||
import binascii
|
||||
import random
|
||||
|
||||
|
||||
def aes_cbc_encrypt(data, key):
|
||||
aes_cipher = AES.new(key, AES.MODE_CBC, '\0' * 16)
|
||||
return aes_cipher.encrypt(data)
|
||||
|
||||
|
||||
def aes_cbc_decrypt(data, key):
|
||||
aes_cipher = AES.new(key, AES.MODE_CBC, '\0' * 16)
|
||||
return aes_cipher.decrypt(data)
|
||||
|
||||
|
||||
def aes_cbc_encrypt_a32(data, key):
|
||||
return str_to_a32(aes_cbc_encrypt(a32_to_str(data), a32_to_str(key)))
|
||||
|
||||
|
||||
def aes_cbc_decrypt_a32(data, key):
|
||||
return str_to_a32(aes_cbc_decrypt(a32_to_str(data), a32_to_str(key)))
|
||||
|
||||
|
||||
def stringhash(str, aeskey):
|
||||
s32 = str_to_a32(str)
|
||||
h32 = [0, 0, 0, 0]
|
||||
|
@ -28,6 +33,7 @@ def stringhash(str, aeskey):
|
|||
h32 = aes_cbc_encrypt_a32(h32, aeskey)
|
||||
return a32_to_base64((h32[0], h32[2]))
|
||||
|
||||
|
||||
def prepare_key(arr):
|
||||
pkey = [0x93C467E3, 0x7DB0C7A4, 0xD1BE3F81, 0x0152CB56]
|
||||
for r in range(0x10000):
|
||||
|
@ -74,6 +80,7 @@ def str_to_a32(b):
|
|||
b += '\0' * (4 - len(b) % 4)
|
||||
return struct.unpack('>%dI' % (len(b) / 4), b)
|
||||
|
||||
|
||||
def mpi_to_int(s):
|
||||
return int(binascii.hexlify(s[2:]), 16)
|
||||
|
||||
|
@ -84,18 +91,22 @@ def base64_url_decode(data):
|
|||
data = data.replace(search, replace)
|
||||
return base64.b64decode(data)
|
||||
|
||||
|
||||
def base64_to_a32(s):
|
||||
return str_to_a32(base64_url_decode(s))
|
||||
|
||||
|
||||
def base64_url_encode(data):
|
||||
data = base64.b64encode(data)
|
||||
for search, replace in (('+', '-'), ('/', '_'), ('=', '')):
|
||||
data = data.replace(search, replace)
|
||||
return data
|
||||
|
||||
|
||||
def a32_to_base64(a):
|
||||
return base64_url_encode(a32_to_str(a))
|
||||
|
||||
|
||||
def get_chunks(size):
|
||||
chunks = {}
|
||||
p = pp = 0
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
|
||||
class ValidationError(Exception):
|
||||
"""
|
||||
Error in validation stage
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class RequestError(Exception):
|
||||
"""
|
||||
Error in API request
|
||||
|
|
48
mega.py
48
mega.py
|
@ -10,12 +10,12 @@ import requests
|
|||
import errors
|
||||
from crypto import *
|
||||
|
||||
class Mega(object):
|
||||
|
||||
class Mega(object):
|
||||
def __init__(self):
|
||||
self.schema = 'https'
|
||||
self.domain = 'mega.co.nz'
|
||||
self.timeout = 160 #max time (secs) to wait for response from api requests
|
||||
self.timeout = 160 #max time (secs) to wait for resp from api requests
|
||||
self.sid = None
|
||||
self.sequence_num = random.randint(0, 0xFFFFFFFF)
|
||||
self.request_id = make_id(10)
|
||||
|
@ -46,7 +46,8 @@ class Mega(object):
|
|||
self.sid = resp['tsid']
|
||||
elif 'csid' in resp:
|
||||
encrypted_rsa_private_key = base64_to_a32(resp['privk'])
|
||||
rsa_private_key = decrypt_key(encrypted_rsa_private_key, self.master_key)
|
||||
rsa_private_key = decrypt_key(encrypted_rsa_private_key,
|
||||
self.master_key)
|
||||
|
||||
private_key = a32_to_str(rsa_private_key)
|
||||
self.rsa_private_key = [0, 0, 0, 0]
|
||||
|
@ -73,8 +74,11 @@ class Mega(object):
|
|||
if self.sid:
|
||||
params.update({'sid': self.sid})
|
||||
req = requests.post(
|
||||
'{0}://g.api.{1}/cs'.format(self.schema,self.domain), params=params, data=json.dumps([data]), timeout=self.timeout)
|
||||
json_resp = req.json()
|
||||
'{0}://g.api.{1}/cs'.format(self.schema, self.domain),
|
||||
params=params,
|
||||
data=json.dumps([data]),
|
||||
timeout=self.timeout)
|
||||
json_resp = json.loads(req.text)
|
||||
|
||||
#if numeric error code response
|
||||
if isinstance(json_resp, int):
|
||||
|
@ -107,7 +111,8 @@ class Mega(object):
|
|||
public_handle,
|
||||
decrypted_key)
|
||||
else:
|
||||
raise ValueError('Upload() response required as input, use get_link() for regular file input')
|
||||
raise ValueError('''Upload() response required as input,
|
||||
use get_link() for regular file input''')
|
||||
|
||||
def get_link(self, file):
|
||||
'''
|
||||
|
@ -199,7 +204,8 @@ class Mega(object):
|
|||
if i['h'] is not u'':
|
||||
node_id = i['h']
|
||||
|
||||
return self.api_request({'a': 'm', 'n': node_id, 't': target_node_id, 'i': self.request_id})
|
||||
return self.api_request({'a': 'm', 'n': node_id, 't': target_node_id,
|
||||
'i': self.request_id})
|
||||
|
||||
def get_node_by_type(self, type):
|
||||
'''
|
||||
|
@ -234,7 +240,9 @@ class Mega(object):
|
|||
attribs = decrypt_attr(attribs, k)
|
||||
file_name = attribs['n']
|
||||
|
||||
print "downloading {0} (size: {1}), url = {2}".format(attribs['n'], file_size, file_url)
|
||||
print "downloading {0} (size: {1}), url = {2}".format(attribs['n'],
|
||||
file_size,
|
||||
file_url)
|
||||
|
||||
input_file = requests.get(file_url, stream=True).raw
|
||||
output_file = open(file_name, 'wb')
|
||||
|
@ -305,18 +313,21 @@ class Mega(object):
|
|||
if len(block) % 16:
|
||||
block += '\0' * (16 - len(block) % 16)
|
||||
block = str_to_a32(block)
|
||||
chunk_mac = [chunk_mac[0] ^ block[0], chunk_mac[1] ^ block[1], chunk_mac[2] ^ block[2],
|
||||
chunk_mac = [chunk_mac[0] ^ block[0], chunk_mac[1] ^ block[1],
|
||||
chunk_mac[2] ^ block[2],
|
||||
chunk_mac[3] ^ block[3]]
|
||||
chunk_mac = aes_cbc_encrypt_a32(chunk_mac, ul_key[:4])
|
||||
|
||||
#our files mac
|
||||
file_mac = [file_mac[0] ^ chunk_mac[0], file_mac[1] ^ chunk_mac[1], file_mac[2] ^ chunk_mac[2],
|
||||
file_mac = [file_mac[0] ^ chunk_mac[0], file_mac[1] ^ chunk_mac[1],
|
||||
file_mac[2] ^ chunk_mac[2],
|
||||
file_mac[3] ^ chunk_mac[3]]
|
||||
file_mac = aes_cbc_encrypt_a32(file_mac, ul_key[:4])
|
||||
|
||||
#encrypt file and upload
|
||||
chunk = aes.encrypt(chunk)
|
||||
output_file = requests.post(ul_url + "/" + str(chunk_start), data=chunk, timeout=self.timeout)
|
||||
output_file = requests.post(ul_url + "/" + str(chunk_start),
|
||||
data=chunk, timeout=self.timeout)
|
||||
completion_file_handle = output_file.text
|
||||
|
||||
#determine meta mac
|
||||
|
@ -324,14 +335,16 @@ class Mega(object):
|
|||
|
||||
attribs = {'n': os.path.basename(filename)}
|
||||
encrypt_attribs = base64_url_encode(encrypt_attr(attribs, ul_key[:4]))
|
||||
key = [ul_key[0] ^ ul_key[4], ul_key[1] ^ ul_key[5], ul_key[2] ^ meta_mac[0], ul_key[3] ^ meta_mac[1],
|
||||
key = [ul_key[0] ^ ul_key[4], ul_key[1] ^ ul_key[5],
|
||||
ul_key[2] ^ meta_mac[0], ul_key[3] ^ meta_mac[1],
|
||||
ul_key[4], ul_key[5], meta_mac[0], meta_mac[1]]
|
||||
encrypted_key = a32_to_base64(encrypt_key(key, self.master_key))
|
||||
#update attributes
|
||||
data = self.api_request({'a': 'p', 't': dest, 'n': [
|
||||
{'h': completion_file_handle, 't': 0, 'a': encrypt_attribs, 'k': encrypted_key}]}
|
||||
)
|
||||
|
||||
data = self.api_request({'a': 'p', 't': dest, 'n': [{
|
||||
'h': completion_file_handle,
|
||||
't': 0,
|
||||
'a': encrypt_attribs,
|
||||
'k': encrypted_key}]})
|
||||
#close input file and return API msg
|
||||
input_file.close()
|
||||
return data
|
||||
|
@ -344,7 +357,8 @@ class Mega(object):
|
|||
key = file['k'][file['k'].index(':') + 1:]
|
||||
key = decrypt_key(base64_to_a32(key), self.master_key)
|
||||
if file['t'] == 0:
|
||||
k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6], key[3] ^ key[7])
|
||||
k = (key[0] ^ key[4], key[1] ^ key[5], key[2] ^ key[6],
|
||||
key[3] ^ key[7])
|
||||
file['iv'] = key[4:6] + (0, 0)
|
||||
file['meta_mac'] = key[6:8]
|
||||
else:
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from mega import Mega
|
||||
|
||||
|
||||
def test():
|
||||
#user details
|
||||
email = 'your@email.com'
|
||||
|
@ -45,5 +46,6 @@ def test():
|
|||
#m.download(file)
|
||||
#m.download_url('https://mega.co.nz/#!6hBW0R4a!By7-Vjj5xal8K5w_IXH3PlGNyZ1VvIrjZkOmHGq1X00')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test()
|
Loading…
Reference in a new issue