function dec_attr($attr, $key) { $attr = trim(aes_cbc_decrypt($attr, a32_to_str($key))); if (substr($attr, 0, 6) != 'MEGA{"') { return false; } return json_decode(substr($attr, 4)); }
function mega_download_file($id, $k, $iv, $meta_mac) { $dl_url = mega_api_req(array('a' => 'g', 'g' => 1, 'p' => $id)); $ch = curl_init($dl_url->g); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, true); $data_enc = curl_exec($ch); curl_close($ch); $data = aes_ctr_decrypt($data_enc, a32_to_str($k), a32_to_str($iv)); file_put_contents('../torrents/test.ipa', $data); /* $file_mac = cbc_mac($data, $k, $iv); print "\nchecking mac\n"; if (array($file_mac[0] ^ $file_mac[1], $file_mac[2] ^ $file_mac[3]) != $meta_mac) { echo "MAC mismatch"; } */ }
function downloadfile($file, $attributes, $k, $iv, $meta_mac) { $dl_url = api_req(array('a' => 'g', 'g' => 1, 'n' => $file->h)); $data_enc = file_get_contents($dl_url->g); $data = aes_ctr_decrypt($data_enc, a32_to_str($k), a32_to_str($iv)); file_put_contents($attributes->n, $data); $file_mac = cbc_mac($data, $k, $iv); if (array($file_mac[0] ^ $file_mac[1], $file_mac[2] ^ $file_mac[3]) != $meta_mac) { echo "MAC mismatch"; } }
function T8_mega_upload($link, $ul_key, $file, $filename, &$mac_str = '') { global $nn, $fp, $fs, $errno, $errstr, $fsize, $pbChunkSize, $T8, $chunk_UL, $calcMacEachChunk, $zapros; $pbChunkSize = GetChunkSize($fsize); $_link = parse_url($link); $scheme = $_link['scheme'] . '://'; $host = $_link['host']; $port = defport($_link); $url = $_link['path'] . (!empty($_link['query']) ? '?' . $_link['query'] : ''); unset($_link); $key = a32_to_str(array_slice($ul_key, 0, 4)); $_td = mcrypt_module_open('rijndael-128', '', 'ctr', ''); $init = mcrypt_generic_init($_td, $key, a32_to_str(array($ul_key[4], $ul_key[5], 0, 0))); if ($init === false || $init < 0) { html_error('Cannot init mcrypt'); } if (!is_readable($file)) { html_error(sprintf(lang(65), $file)); } echo "\n<p id='ul_con'></p>\n<p id='ul_fname' style='display:none'>" . lang(104) . " <b>{$filename}</b>, " . lang(56) . ' <b>' . bytesToKbOrMbOrGb($fsize) . "</b>...<br /></p><p id='T8' style='display:none'></p>\n"; flush(); if ($chunk_UL) { global $chunkSize, $timeStart, $data, $totalsend, $time, $lastChunkTime; } else { chunk_ul($scheme, $host, $port, $url, true); } $fs = fopen($file, 'rb'); $chunkSize = 131072; $totalsend = $time = $lastChunkTime = 0; $_data = ''; if ($calcMacEachChunk) { global $m_td1, $m_td2; $m_td1 = mcrypt_module_open('rijndael-128', '', 'cbc', ''); $m_td2 = mcrypt_module_open('rijndael-128', '', 'cbc', ''); $init = mcrypt_generic_init($m_td1, $key, ""); if ($init === false || $init < 0) { html_error('Cannot init mcrypt'); } $c_mac = a32_to_str(array($ul_key[4], $ul_key[5], $ul_key[4], $ul_key[5])); } $timeStart = microtime(true); while (!feof($fs) && !$errno && !$errstr) { $data = fread($fs, $chunkSize); if ($data === false) { if (isset($GLOBALS['T8DEBUG'])) { textarea("UPLOAD\nRAW Request:\n" . $zapros . "[Incomplete File Content]", 200, 15); } //Debug fclose($fs); if (!$chunk_UL) { fclose($fp); } html_error(lang(112)); } if ($_data !== '') { $data = $_data . $data; $_data = ''; } if (strlen($data) < $chunkSize && !feof($fs)) { $_data .= $data; continue; } if (strlen($data) > $chunkSize) { $_data .= substr($data, $chunkSize); $data = substr($data, 0, $chunkSize); } if ($calcMacEachChunk) { $mac_str = calcChunkMac($data, $key, $c_mac); } if ($chunkSize < 1048576) { $chunkSize = getNextChunkLength($chunkSize); } $data = mcrypt_generic($_td, $data); if ($chunk_UL) { $page = chunk_ul($scheme, $host, $port, "{$url}/{$totalsend}-" . ($totalsend + strlen($data) - 1)); } else { $dlen = strlen($data); $sended = 0; for ($s = 0; $s < $dlen - 1; $s += $pbChunkSize) { $chunk = $pbChunkSize >= $dlen - $s ? substr($data, $s) : substr($data, $s, $pbChunkSize); $sendbyte = @fputs($fp, $chunk); fflush($fp); if ($sendbyte === false || strlen($chunk) > $sendbyte) { if (isset($GLOBALS['T8DEBUG'])) { textarea("UPLOAD\n{$errstr}\n" . print_r(stream_get_meta_data($fp), true) . "\nRAW Request:\n" . $zapros . "[Incomplete File Content]", 200, 15); } //Debug fclose($fs); fclose($fp); html_error(lang(113)); } $totalsend += $sendbyte; $sended += $sendbyte; $time = getmicrotime() - $timeStart; $chunkTime = $time - $lastChunkTime; if ($s + $sendbyte <= $dlen - 1 && $chunkTime < 1) { continue; } $chunkTime = !($chunkTime < 0) && $chunkTime > 0 ? $chunkTime : 1; $lastChunkTime = $time; $speed = round($sended / 1024 / $chunkTime, 2); $percent = round($totalsend / $fsize * 100, 2); echo "<script type='text/javascript'>pr('{$percent}', '" . bytesToKbOrMbOrGb($totalsend) . "', '{$speed}');</script>\n"; flush(); $sended = 0; } } } mcrypt_generic_deinit($_td); mcrypt_module_close($_td); if ($errno || $errstr) { $lastError = $errstr; return false; } if (!$chunk_UL) { fflush($fp); $page = ''; while (!feof($fp)) { $data = fgets($fp, 16384); if ($data === false) { break; } $page .= $data; } fclose($fp); } fclose($fs); if (isset($GLOBALS['T8DEBUG']) && !$chunk_UL) { textarea("UPLOAD\nResponse:\n{$page}\nRAW Request:\n" . $zapros . "[Encrypted Uploaded File Content]", 200, 15); } //Debug return $page; }