public async Task <Node> CreateFolder(string name, Node parent, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (parent == null) { throw new ArgumentNullException("parent"); } if (parent.Type == NodeType.File) { throw new ArgumentException("Invalid parent node"); } //this.EnsureLoggedIn(); var key = Crypto.CreateAesKey(); byte[] attributes = EncryptAttributes(new Attributes(name), key); byte[] encryptedKey = Crypto.EncryptAes(key, this._masterKey); CreateNodeRequest request = CreateNodeRequest.CreateFolderNodeRequest(parent, attributes.ToBase64(), encryptedKey.ToBase64()); GetNodesResponse response = await Post <CreateNodeRequest, GetNodesResponse>(request, cancellationToken); var node = response.Nodes[0]; DecriptNode(node, response); return(node); }
public async Task <Node> Upload(Stream fileContent, string name, Node parent, IProgress <StreamProgress> progress, CancellationToken cancellationToken) { if (fileContent == null) { throw new ArgumentNullException("fileContent"); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (parent == null) { throw new ArgumentNullException("parent"); } if (parent.Type == NodeType.File) { throw new ArgumentException("Invalid parent node"); } //this.EnsureLoggedIn(); // Retrieve upload URL var length = fileContent.GetLength(); if (!length.HasValue) { fileContent = new MemoryStream(await fileContent.ReadAsBufferAsync(cancellationToken)); length = fileContent.GetLength(); } UploadUrlRequest uploadRequest = new UploadUrlRequest(length.Value); UploadUrlResponse uploadResponse = await Post <UploadUrlRequest, UploadUrlResponse>(uploadRequest, cancellationToken); var megaStream = new MegaEncryptStream(fileContent); string completionHandle; try { completionHandle = await PostRaw(new Uri(uploadResponse.Url), megaStream, progress, cancellationToken); } finally { megaStream.Dispose(); } // Encrypt attributes byte[] cryptedAttributes = EncryptAttributes(new Attributes(name), megaStream.FileKey); // Compute the file key byte[] fileKey = new byte[32]; for (int i = 0; i < 8; i++) { fileKey[i] = (byte)(megaStream.FileKey[i] ^ megaStream.IV[i]); fileKey[i + 16] = megaStream.IV[i]; } for (int i = 8; i < 16; i++) { fileKey[i] = (byte)(megaStream.FileKey[i] ^ megaStream.MetaMac[i - 8]); fileKey[i + 16] = megaStream.MetaMac[i - 8]; } byte[] encryptedKey = Crypto.EncryptKey(fileKey, this._masterKey); CreateNodeRequest createNodeRequest = CreateNodeRequest.CreateFileNodeRequest(parent, cryptedAttributes.ToBase64(), encryptedKey.ToBase64(), completionHandle); GetNodesResponse createNodeResponse = await Post <CreateNodeRequest, GetNodesResponse>(createNodeRequest, cancellationToken); var node = createNodeResponse.Nodes[0]; DecriptNode(node, createNodeResponse); return(node); }