Esempio n. 1
0
        public Stream Download(Uri uri, CancellationToken?cancellationToken = null)
#endif
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            this.EnsureLoggedIn();

            string id;

            byte[] iv, metaMac, key;
            this.GetPartsFromUri(uri, out id, out iv, out metaMac, out key);

            // Retrieve download URL
            DownloadUrlRequestFromId downloadRequest  = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = this.Request <DownloadUrlResponse>(downloadRequest);

            Stream dataStream = this.webClient.GetRequestRaw(new Uri(downloadResponse.Url));

            Stream resultStream = new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, key, iv, metaMac);

#if !NET35
            if (cancellationToken.HasValue)
            {
                resultStream = new CancellableStream(resultStream, cancellationToken.Value);
            }
#endif
            return(resultStream);
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieve a Stream to download and decrypt the specified Uri
        /// </summary>
        /// <param name="uri">Uri to download</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">uri is null</exception>
        /// <exception cref="ArgumentException">Uri is not valid (id and key are required)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream Download(Uri uri, CancellationToken?cancellationToken = null)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            this.EnsureLoggedIn();

            string id;

            byte[] iv, metaMac, key;
            this.GetPartsFromUri(uri, out id, out iv, out metaMac, out key);

            // Retrieve download URL
            DownloadUrlRequestFromId downloadRequest  = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = this.Request <DownloadUrlResponse>(downloadRequest);

            Uri    downloadUrl = new Uri(downloadResponse.Url);
            Stream dataStream  = new SeekableReadStream(this.webClient.GetLength(downloadUrl), (buffer, bufferOffset, offset, count)
                                                        => this.webClient.GetRequestRawWithRange(
                                                            downloadUrl, offset, offset + count).Read(buffer, bufferOffset, count));

            Stream resultStream = new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, key, iv, metaMac);

            if (cancellationToken.HasValue)
            {
                resultStream = new CancellableStream(resultStream, cancellationToken.Value);
            }

            return(resultStream);
        }
Esempio n. 3
0
        /// <summary>
        /// Retrieve a Stream to download and decrypt the specified Uri
        /// </summary>
        /// <param name="uri">Uri to download</param>
        /// <param name="dataSize">Fill</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">uri is null</exception>
        /// <exception cref="ArgumentException">Uri is not valid (id and key are required)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream Download(Uri uri, ref long dataSize)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            this.EnsureLoggedIn();

            Regex uriRegex = new Regex("#!(?<id>.+)!(?<key>.+)");
            Match match    = uriRegex.Match(uri.Fragment);

            if (match.Success == false)
            {
                throw new ArgumentException(string.Format("Invalid uri. Unable to extract Id and Key from the uri {0}", uri));
            }

            string id = match.Groups["id"].Value;

            byte[] decryptedKey = match.Groups["key"].Value.FromBase64();

            byte[] iv;
            byte[] metaMac;
            byte[] fileKey;
            Crypto.GetPartsFromDecryptedKey(decryptedKey, out iv, out metaMac, out fileKey);

            // Retrieve download URL
            DownloadUrlRequestFromId downloadRequest  = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = this.Request <DownloadUrlResponse>(downloadRequest);

            Stream dataStream = this._webClient.GetRequestRaw(new Uri(downloadResponse.Url));

            dataSize = downloadResponse.Size;
            return(new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, fileKey, iv, metaMac));
        }
Esempio n. 4
0
        // Token: 0x06000949 RID: 2377 RVA: 0x0004BCC8 File Offset: 0x00049EC8
        public INodeInfo GetNodeFromLink(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            this.EnsureLoggedIn();
            string id;

            byte[] array;
            byte[] array2;
            byte[] key;
            this.GetPartsFromUri(uri, out id, out array, out array2, out key);
            DownloadUrlRequestFromId request          = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = this.Request <DownloadUrlResponse>(request, null);

            return(new NodeInfo(id, downloadResponse, key));
        }
Esempio n. 5
0
        /// <summary>
        /// Retrieve public properties of a file from a specified Uri
        /// </summary>
        /// <param name="uri">Uri to retrive properties</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">uri is null</exception>
        /// <exception cref="ArgumentException">Uri is not valid (id and key are required)</exception>
        public INodeInfo GetNodeFromLink(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            this.EnsureLoggedIn();

            string id;

            byte[] iv, metaMac, key;
            this.GetPartsFromUri(uri, out id, out iv, out metaMac, out key);

            // Retrieve attributes
            DownloadUrlRequestFromId downloadRequest  = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = this.Request <DownloadUrlResponse>(downloadRequest);

            return(new NodeInfo(id, downloadResponse, key));
        }
        /// <summary>
        /// Retrieve a Stream to download and decrypt the specified Uri
        /// </summary>
        /// <param name="uri">Uri to download</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">uri is null</exception>
        /// <exception cref="ArgumentException">Uri is not valid (id and key are required)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream Download(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            this.EnsureLoggedIn();

            string id;

            byte[] iv, metaMac, fileKey;
            this.GetPartsFromUri(uri, out id, out iv, out metaMac, out fileKey);

            // Retrieve download URL
            DownloadUrlRequestFromId downloadRequest  = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = this.Request <DownloadUrlResponse>(downloadRequest);

            Stream dataStream = this.webClient.GetRequestRaw(new Uri(downloadResponse.Url));

            return(new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, fileKey, iv, metaMac));
        }
Esempio n. 7
0
        // Token: 0x06000948 RID: 2376 RVA: 0x0004BC34 File Offset: 0x00049E34
        public Stream Download(Uri uri, CancellationToken?cancellationToken = null)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            this.EnsureLoggedIn();
            string id;

            byte[] iv;
            byte[] expectedMetaMac;
            byte[] fileKey;
            this.GetPartsFromUri(uri, out id, out iv, out expectedMetaMac, out fileKey);
            DownloadUrlRequestFromId request             = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadUrlResponse = this.Request <DownloadUrlResponse>(request, null);
            Stream stream = new MegaAesCtrStreamDecrypter(new BufferedStream(this.webClient.GetRequestRaw(new Uri(downloadUrlResponse.Url))), downloadUrlResponse.Size, fileKey, iv, expectedMetaMac);

            if (cancellationToken != null)
            {
                stream = new CancellableStream(stream, cancellationToken.Value);
            }
            return(stream);
        }