Example #1
0
        // Token: 0x06000947 RID: 2375 RVA: 0x0004BB88 File Offset: 0x00049D88
        public Stream Download(INode node, CancellationToken?cancellationToken = null)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (node.Type != NodeType.File)
            {
                throw new ArgumentException("Invalid node");
            }
            INodeCrypto nodeCrypto = node as INodeCrypto;

            if (nodeCrypto == null)
            {
                throw new ArgumentException("node must implement INodeCrypto");
            }
            this.EnsureLoggedIn();
            DownloadUrlRequest  request             = new DownloadUrlRequest(node);
            DownloadUrlResponse downloadUrlResponse = this.Request <DownloadUrlResponse>(request, null);
            Stream stream = new MegaAesCtrStreamDecrypter(new BufferedStream(this.webClient.GetRequestRaw(new Uri(downloadUrlResponse.Url))), downloadUrlResponse.Size, nodeCrypto.Key, nodeCrypto.Iv, nodeCrypto.MetaMac);

            if (cancellationToken != null)
            {
                stream = new CancellableStream(stream, cancellationToken.Value);
            }
            return(stream);
        }
Example #2
0
 internal NodeInfo(string id, DownloadUrlResponse downloadResponse, byte[] key)
 {
     this.Id         = id;
     this.Attributes = Crypto.DecryptAttributes(downloadResponse.SerializedAttributes.FromBase64(), key);
     this.Size       = downloadResponse.Size;
     this.Type       = NodeType.File;
 }
Example #3
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);
        }
        /// <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);
        }
        /// <summary>
        /// Retrieve a Stream to download and decrypt the specified node
        /// </summary>
        /// <param name="node">Node to download (only <see cref="NodeType.File" /> can be downloaded)</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">node or outputFile is null</exception>
        /// <exception cref="ArgumentException">node is not valid (only <see cref="NodeType.File" /> can be downloaded)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream Download(INode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.Type != NodeType.File)
            {
                throw new ArgumentException("Invalid node");
            }

            INodeCrypto nodeCrypto = node as INodeCrypto;

            if (nodeCrypto == null)
            {
                throw new ArgumentException("node must implement INodeCrypto");
            }

            this.EnsureLoggedIn();

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

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

            return(new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, nodeCrypto.Key, nodeCrypto.Iv, nodeCrypto.MetaMac));
        }
Example #6
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));
        }
Example #7
0
        public NodePublic(DownloadUrlResponse downloadResponse, byte[] fileKey)
        {
            Attributes attributes = Crypto.DecryptAttributes(downloadResponse.SerializedAttributes.FromBase64(), fileKey);

            this.Name = attributes.Name;
            this.Size = downloadResponse.Size;
            this.Type = NodeType.File;
        }
Example #8
0
 internal Node(string id, DownloadUrlResponse downloadResponse, byte[] key, byte[] iv, byte[] metaMac)
 {
     Id             = id;
     Attributes     = Crypto.DecryptAttributes(downloadResponse.SerializedAttributes.FromBase64(), key);
     Size           = downloadResponse.Size;
     Type           = NodeType.File;
     FileAttributes = DeserializeFileAttributes(downloadResponse.SerializedFileAttributes);
     Key            = key;
     Iv             = iv;
     MetaMac        = metaMac;
 }
Example #9
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));
        }
Example #10
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));
        }
        /// <summary>
        /// Retrieve a Stream to download and decrypt the specified node
        /// </summary>
        /// <param name="node">Node to download (only <see cref="NodeType.File" /> can be downloaded)</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">node or outputFile is null</exception>
        /// <exception cref="ArgumentException">node is not valid (only <see cref="NodeType.File" /> can be downloaded)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream Download(INode node, CancellationToken?cancellationToken = null)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.Type != NodeType.File)
            {
                throw new ArgumentException("Invalid node");
            }

            INodeCrypto nodeCrypto = node as INodeCrypto;

            if (nodeCrypto == null)
            {
                throw new ArgumentException("node must implement INodeCrypto");
            }

            this.EnsureLoggedIn();

            // Retrieve download URL
            DownloadUrlRequest  downloadRequest  = new DownloadUrlRequest(node);
            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, nodeCrypto.Key, nodeCrypto.Iv, nodeCrypto.MetaMac);

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

            return(resultStream);
        }
Example #13
0
        public Stream Download(INode node, CancellationToken?cancellationToken = null)
#endif
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.Type != NodeType.File)
            {
                throw new ArgumentException("Invalid node");
            }

            INodeCrypto nodeCrypto = node as INodeCrypto;

            if (nodeCrypto == null)
            {
                throw new ArgumentException("node must implement INodeCrypto");
            }

            this.EnsureLoggedIn();

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

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

            Stream resultStream = new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, nodeCrypto.Key, nodeCrypto.Iv, nodeCrypto.MetaMac);

#if !NET35
            if (cancellationToken.HasValue)
            {
                resultStream = new CancellableStream(resultStream, cancellationToken.Value);
            }
#endif
            return(resultStream);
        }
Example #14
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);
        }
Example #15
0
        /// <summary>
        /// Retrieve a Stream to download and decrypt the file from specified Share Uri
        /// </summary>
        /// <param name="uri">Share Uri</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 (only file can be downloaded)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream DownloadFromUrl(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            this.EnsureLoggedIn();

            string[] url = uri.OriginalString.Split('!');
            if (url[0].EndsWith("F"))
            {
                throw new ArgumentException("Share Uri is a directory");
            }
            Byte[] decryptedKey = url[2].FromBase64();

            // Extract Iv and MetaMac
            byte[] iv      = new byte[8];
            byte[] metaMac = new byte[8];
            Array.Copy(decryptedKey, 16, iv, 0, 8);
            Array.Copy(decryptedKey, 24, metaMac, 0, 8);

            // For files, key is 256 bits long. Compute the key to retrieve 128 AES key
            Byte[] key = new byte[16];
            for (int idx = 0; idx < 16; idx++)
            {
                key[idx] = (byte)(decryptedKey[idx] ^ decryptedKey[idx + 16]);
            }

            // Retrieve download URL
            ShareFileDownloadUrlRequest downloadRequest  = new ShareFileDownloadUrlRequest(url[1]);
            DownloadUrlResponse         downloadResponse = this.Request <DownloadUrlResponse>(downloadRequest);

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

            return(new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, key, iv, metaMac));
        }