Removed dupe functions in crpyto, input file close, added example file output to test.py

master
richard@richard.do 2013-02-06 15:55:29 +00:00
parent 0d1acf6e99
commit 321aa6524b
3 changed files with 9 additions and 15 deletions

View File

@ -61,18 +61,12 @@ def encrypt_attr(attr, key):
def decrypt_attr(attr, key): def decrypt_attr(attr, key):
attr = aes_cbc_decrypt(attr, a32_to_str(key)).rstrip('\0') attr = aes_cbc_decrypt(attr, a32_to_str(key)).rstrip('\0')
return json.loads(attr[4:]) return json.loads(attr[4:]) if attr[:6] == 'MEGA{"' else False
def a32_to_str(a): def a32_to_str(a):
return struct.pack('>%dI' % len(a), *a) return struct.pack('>%dI' % len(a), *a)
def aes_cbc_encrypt(data, key):
aes = AES.new(key, AES.MODE_CBC, '\0' * 16)
return aes.encrypt(data)
def aes_cbc_encrypt_a32(data, key):
return str_to_a32(aes_cbc_encrypt(a32_to_str(data), a32_to_str(key)))
def str_to_a32(b): def str_to_a32(b):
if len(b) % 4: if len(b) % 4:
@ -83,12 +77,6 @@ def str_to_a32(b):
def mpi_to_int(s): def mpi_to_int(s):
return int(binascii.hexlify(s[2:]), 16) return int(binascii.hexlify(s[2:]), 16)
def aes_cbc_decrypt(data, key):
decryptor = AES.new(key, AES.MODE_CBC, '\0' * 16)
return decryptor.decrypt(data)
def aes_cbc_decrypt_a32(data, key):
return str_to_a32(aes_cbc_decrypt(a32_to_str(data), a32_to_str(key)))
def base64_url_decode(data): def base64_url_decode(data):
data += '=='[(2 - len(data) * 3) % 4:] data += '=='[(2 - len(data) * 3) % 4:]
@ -136,4 +124,4 @@ def make_id(length):
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for i in range(length): for i in range(length):
text += random.choice(possible) text += random.choice(possible)
return text return text

View File

@ -292,6 +292,9 @@ class Mega(object):
data = self.api_request({'a': 'p', 't': dest, 'n': [ data = self.api_request({'a': 'p', 't': dest, 'n': [
{'h': completion_file_handle, 't': 0, 'a': encrypt_attribs, 'k': encrypted_key}]} {'h': completion_file_handle, 't': 0, 'a': encrypt_attribs, 'k': encrypted_key}]}
) )
#close input file and return API msg
input_file.close()
return data return data
def process_file(self, file): def process_file(self, file):

View File

@ -16,7 +16,10 @@ def test():
#get account files #get account files
files = m.get_files() files = m.get_files()
print(files) #example iterate over files
for file in files:
if files[file]['a'] != False:
print files[file]
#upload file #upload file
print(m.upload('test.py')) print(m.upload('test.py'))