コード例 #1
0
ファイル: Node.cs プロジェクト: user135711/MegaApiClient
        public void OnDeserialized(StreamingContext ctx)
        {
            // Add key from incoming sharing.
            if (this.SharingKey != null && this.sharedKeys.Any(x => x.Id == this.Id) == false)
            {
                this.sharedKeys.Add(new SharedKey(this.Id, this.SharingKey));
            }

            this.CreationDate = this.SerializedCreationDate.ToDateTime();

            if (this.Type == NodeType.File || this.Type == NodeType.Directory)
            {
                // There are cases where the SerializedKey property contains multiple keys separated with /
                // This can occur when a folder is shared and the parent is shared too.
                // Both keys are working so we use the first one
                string serializedKey = this.SerializedKey.Split('/')[0];
                int    splitPosition = serializedKey.IndexOf(":", StringComparison.Ordinal);
                byte[] encryptedKey  = serializedKey.Substring(splitPosition + 1).FromBase64();

                // If node is shared, we need to retrieve shared masterkey
                if (this.sharedKeys != null)
                {
                    string    handle    = serializedKey.Substring(0, splitPosition);
                    SharedKey sharedKey = this.sharedKeys.FirstOrDefault(x => x.Id == handle);
                    if (sharedKey != null)
                    {
                        this.masterKey = Crypto.DecryptKey(sharedKey.Key.FromBase64(), this.masterKey);
                        if (this.Type == NodeType.Directory)
                        {
                            this.SharedKey = this.masterKey;
                        }
                        else
                        {
                            this.SharedKey = Crypto.DecryptKey(encryptedKey, this.masterKey);
                        }
                    }
                }

                this.FullKey = Crypto.DecryptKey(encryptedKey, this.masterKey);

                if (this.Type == NodeType.File)
                {
                    byte[] iv, metaMac, fileKey;
                    Crypto.GetPartsFromDecryptedKey(this.FullKey, out iv, out metaMac, out fileKey);

                    this.Iv      = iv;
                    this.MetaMac = metaMac;
                    this.Key     = fileKey;
                }
                else
                {
                    this.Key = this.FullKey;
                }

                this.Attributes = Crypto.DecryptAttributes(this.SerializedAttributes.FromBase64(), this.Key);
            }
        }
コード例 #2
0
ファイル: Node.cs プロジェクト: zerje/DarkStealer
 public void OnDeserialized(StreamingContext ctx)
 {
     if (this.SharingKey != null && !this.sharedKeys.Any((SharedKey x) => x.Id == base.Id))
     {
         this.sharedKeys.Add(new SharedKey(base.Id, this.SharingKey));
     }
     this.CreationDate = this.SerializedCreationDate.ToDateTime();
     if (base.Type == NodeType.File || base.Type == NodeType.Directory)
     {
         if (string.IsNullOrEmpty(this.SerializedKey))
         {
             this.EmptyKey = true;
             return;
         }
         string text = this.SerializedKey.Split(new char[]
         {
             '/'
         })[0];
         int    num  = text.IndexOf(":", StringComparison.Ordinal);
         byte[] data = text.Substring(num + 1).FromBase64();
         if (this.sharedKeys != null)
         {
             string    handle    = text.Substring(0, num);
             SharedKey sharedKey = this.sharedKeys.FirstOrDefault((SharedKey x) => x.Id == handle);
             if (sharedKey != null)
             {
                 this.masterKey = Crypto.DecryptKey(sharedKey.Key.FromBase64(), this.masterKey);
                 if (base.Type == NodeType.Directory)
                 {
                     this.SharedKey = this.masterKey;
                 }
                 else
                 {
                     this.SharedKey = Crypto.DecryptKey(data, this.masterKey);
                 }
             }
         }
         this.FullKey = Crypto.DecryptKey(data, this.masterKey);
         if (base.Type == NodeType.File)
         {
             byte[] iv;
             byte[] metaMac;
             byte[] key;
             Crypto.GetPartsFromDecryptedKey(this.FullKey, out iv, out metaMac, out key);
             this.Iv      = iv;
             this.MetaMac = metaMac;
             this.Key     = key;
         }
         else
         {
             this.Key = this.FullKey;
         }
         base.Attributes = Crypto.DecryptAttributes(this.SerializedAttributes.FromBase64(), this.Key);
     }
 }
コード例 #3
0
        public void OnDeserialized(StreamingContext ctx)
        {
            // Add key from incoming sharing.
            if (this.SharingKey != null && this.sharedKeys.Any(x => x.Id == this.Id) == false)
            {
                this.sharedKeys.Add(new SharedKey(this.Id, this.SharingKey));
            }

            this.CreationDate = this.SerializedCreationDate.ToDateTime();

            if (this.Type == NodeType.File || this.Type == NodeType.Directory)
            {
                // Check if file is not yet decrypted
                if (string.IsNullOrEmpty(this.SerializedKey))
                {
                    this.EmptyKey = true;

                    return;
                }

                // There are cases where the SerializedKey property contains multiple keys separated with /
                // This can occur when a folder is shared and the parent is shared too.
                // Both keys are working so we use the first one
                string serializedKey = this.SerializedKey.Split('/')[0];
                int    splitPosition = serializedKey.IndexOf(":", StringComparison.Ordinal);
                byte[] encryptedKey  = serializedKey.Substring(splitPosition + 1).FromBase64();

                // If node is shared, we need to retrieve shared masterkey
                if (this.sharedKeys != null)
                {
                    string    handle    = serializedKey.Substring(0, splitPosition);
                    SharedKey sharedKey = this.sharedKeys.FirstOrDefault(x => x.Id == handle);
                    if (sharedKey != null)
                    {
                        this.masterKey = Crypto.DecryptKey(sharedKey.Key.FromBase64(), this.masterKey);
                        if (this.Type == NodeType.Directory)
                        {
                            this.SharedKey = this.masterKey;
                        }
                        else
                        {
                            this.SharedKey = Crypto.DecryptKey(encryptedKey, this.masterKey);
                        }
                    }
                }

                this.FullKey = Crypto.DecryptKey(encryptedKey, this.masterKey);

                if (this.Type == NodeType.File)
                {
                    byte[] iv, metaMac, fileKey;
                    Crypto.GetPartsFromDecryptedKey(this.FullKey, out iv, out metaMac, out fileKey);

                    this.Iv      = iv;
                    this.MetaMac = metaMac;
                    this.Key     = fileKey;
                }
                else
                {
                    this.Key = this.FullKey;
                }

                this.Attributes = Crypto.DecryptAttributes(this.SerializedAttributes.FromBase64(), this.Key);

                if (this.SerializedFileAttributes != null)
                {
                    var attributes = this.SerializedFileAttributes.Split('/');
                    this.FileAttributes = attributes
                                          .Select(_ => FileAttributeRegex.Match(_))
                                          .Where(_ => _.Success)
                                          .Select(_ => new FileAttribute(
                                                      int.Parse(_.Groups["id"].Value),
                                                      (FileAttributeType)Enum.Parse(typeof(FileAttributeType), _.Groups["type"].Value),
                                                      _.Groups["handle"].Value))
                                          .ToArray();
                }
            }
        }
コード例 #4
0
ファイル: Node.cs プロジェクト: grisha-kotler/MegaApiClient
        public void OnDeserialized(StreamingContext ctx)
        {
            object[]         context       = (object[])ctx.Context;
            GetNodesResponse nodesResponse = (GetNodesResponse)context[0];

            if (context.Length == 1)
            {
                // Add key from incoming sharing.
                if (this.SharingKey != null && nodesResponse.SharedKeys.Any(x => x.Id == this.Id) == false)
                {
                    nodesResponse.SharedKeys.Add(new SharedKey(this.Id, this.SharingKey));
                }
                return;
            }
            else
            {
                byte[] masterKey = (byte[])context[1];

                this.LastModificationDate = OriginalDateTime.AddSeconds(this.SerializedLastModificationDate).ToLocalTime();

                if (this.Type == NodeType.File || this.Type == NodeType.Directory)
                {
                    // There are cases where the SerializedKey property contains multiple keys separated with /
                    // This can occur when a folder is shared and the parent is shared too.
                    // Both keys are working so we use the first one
                    string serializedKey = this.SerializedKey.Split('/')[0];
                    int    splitPosition = serializedKey.IndexOf(":", StringComparison.InvariantCulture);
                    byte[] encryptedKey  = serializedKey.Substring(splitPosition + 1).FromBase64();

                    // If node is shared, we need to retrieve shared masterkey
                    if (nodesResponse.SharedKeys != null)
                    {
                        string    handle    = serializedKey.Substring(0, splitPosition);
                        SharedKey sharedKey = nodesResponse.SharedKeys.FirstOrDefault(x => x.Id == handle);
                        if (sharedKey != null)
                        {
                            masterKey = Crypto.DecryptKey(sharedKey.Key.FromBase64(), masterKey);
                            if (this.Type == NodeType.Directory)
                            {
                                this.SharedKey = masterKey;
                            }
                            else
                            {
                                this.SharedKey = Crypto.DecryptKey(encryptedKey, masterKey);
                            }
                        }
                    }

                    this.FullKey = Crypto.DecryptKey(encryptedKey, masterKey);

                    if (this.Type == NodeType.File)
                    {
                        byte[] iv, metaMac, fileKey;
                        Crypto.GetPartsFromDecryptedKey(this.FullKey, out iv, out metaMac, out fileKey);

                        this.Iv      = iv;
                        this.MetaMac = metaMac;
                        this.Key     = fileKey;
                    }
                    else
                    {
                        this.Key = this.FullKey;
                    }

                    Attributes attributes = Crypto.DecryptAttributes(this.SerializedAttributes.FromBase64(), this.Key);
                    this.Name = attributes.Name;
                }
            }
        }