private static function _encryptLink($link, array $options = []) { if (preg_match('/^.*?!(?P<file_id>[^!]+)!(?P<file_key>.+)$/', trim($link), $match)) { $secret = base64_encode(openssl_random_pseudo_bytes(self::SECRET_BYTE_LENGTH)); /* ¡OJO! -> NO SE PUEDE CAMBIAR EL ORDEN NI ELIMINAR NINGUNO */ $extra = implode(self::SEPARATOR_EXTRA, [!empty($options['extra_info']) ? base64_encode(substr($options['extra_info'], 0, self::MAX_EXTRA_BYTES)) : null, $options['hide_name'] ? self::EXTRA_TRUE_CHAR : null, is_numeric($options['expire']) && time() < (int) $options['expire'] ? (int) $options['expire'] : null, !empty($options['referer']) ? base64_encode(substr($options['referer'], 0, self::MAX_REFERER_BYTES)) : null, !empty($options['email']) ? base64_encode(substr($options['email'], 0, self::MAX_EMAIL_BYTES)) : null, !empty($options['zombie']) ? $options['zombie'] : null, $options['no_expire_token'] ? self::EXTRA_TRUE_CHAR : null]); $data = Utils_MiscTools::urlBase64Encode(Utils_CryptTools::aesCbcEncrypt(gzdeflate(implode(self::SEPARATOR, [$secret, $match['file_id'], $match['file_key'], !empty($options['pass']) ? self::PASS_HASH_ITERATIONS_LOG2 . '#' . base64_encode(Utils_CryptTools::passHMAC('sha256', $options['pass'], $salt = openssl_random_pseudo_bytes(self::PASS_SALT_BYTE_LENGTH), pow(2, self::PASS_HASH_ITERATIONS_LOG2))) . '#' . base64_encode($salt) : null, $extra, !empty($options['auth']) ? $options['auth'] : null]), 9), Utils_MiscTools::hex2bin(MASTER_KEY), md5(MASTER_KEY, true))); $hash = hash_hmac(self::HMAC_ALGO, $data, md5(MASTER_KEY)); $url_path = preg_replace('/.{' . self::MAX_FILE_NAME_BYTES . '}(?!$)/', '\\0/', "!{$data}!{$hash}"); $c_link = URL_BASE . "/{$url_path}"; return ['link' => $options['tiny_url'] ? Utils_MiscTools::deflateUrl($c_link) : $c_link, 'secret' => $secret]; } else { throw new Exception_MegaCrypterLinkException(self::LINK_ERROR); } }