def stringhash(s, aeskey): s32 = str_to_a32(s) h32 = [0, 0, 0, 0] for i in xrange(len(s32)): h32[i % 4] ^= s32[i] for _ in xrange(0x4000): h32 = aes_cbc_encrypt_a32(h32, aeskey) return a32_to_base64((h32[0], h32[2]))
def stringhash(s, aeskey): s32 = str_to_a32(s) h32 = [0, 0, 0, 0] for i in range(len(s32)): h32[i % 4] ^= s32[i] for _ in range(0x4000): h32 = aes_cbc_encrypt_a32(h32, aeskey) return a32_to_base64((h32[0], h32[2]))
def login_ephemeral(self): random_master_key = [random.randint(0, 0xFFFFFFFF)] * 4 random_password_key = [random.randint(0, 0xFFFFFFFF)] * 4 random_session_self_challenge = [random.randint(0, 0xFFFFFFFF)] * 4 user_handle = self.api_req({ 'a': 'up', 'k': a32_to_base64(encrypt_key(random_master_key, random_password_key)), 'ts': base64urlencode(a32_to_str(random_session_self_challenge) + a32_to_str(encrypt_key( random_session_self_challenge, random_master_key))) }) res = self.api_req({'a': 'us', 'user': user_handle}) self._login_common(res, random_password_key)
def uploadfile(self, filename, dst=None): if not dst: 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 range(6)] counter = Counter.new( 128, initial_value=((ul_key[4] << 32) + ul_key[5]) << 64) encryptor = AES.new( a32_to_str(ul_key[:4]), AES.MODE_CTR, counter=counter) file_mac = [0, 0, 0, 0] for chunk_start, chunk_size in sorted(get_chunks(size).items()): chunk = infile.read(chunk_size) chunk_mac = [ul_key[4], ul_key[5], ul_key[4], ul_key[5]] for i in range(0, len(chunk), 16): block = chunk[i:i+16] 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[3] ^ block[3]] chunk_mac = aes_cbc_encrypt_a32(chunk_mac, ul_key[:4]) 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]) chunk = encryptor.encrypt(chunk) url = '%s/%s' % (ul_url, str(chunk_start)) outfile = requests.post(url, data=chunk, stream=True).raw completion_handle = outfile.read() infile.close() meta_mac = (file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3]) attributes = {'n': os.path.basename(filename)} enc_attributes = base64urlencode(enc_attr(attributes, 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], ul_key[4], ul_key[5], meta_mac[0], meta_mac[1]] encrypted_key = a32_to_base64(encrypt_key(key, self.master_key)) data = self.api_req({'a': 'p', 't': dst, 'n': [ {'h': completion_handle, 't': 0, 'a': enc_attributes, 'k': encrypted_key}]}) return data
def get_public_url(self, file_id, file_key): public_handle = self.api_req({'a': 'l', 'n': file_id}) decrypted_key = a32_to_base64(file_key) return 'http://mega.co.nz/#!%s!%s' % (public_handle, decrypted_key)