private void RetrieveRessourceTable(int counter = 0)
        {
            if (!_keyRetrieved)
            {
                RetrieveKey();
            }

            if (counter == 3)
            {
                ClientContext.Logger.LogWarning($"Failed to reach API! ({counter} retries!)", "", "", 0);
                return;
            }

            try {
                using (WebClient webClient = new WebClient())
                    using (MemoryStream input = new MemoryStream(webClient.DownloadData($"http://{RandomProvider()}/0000000000000000-1")))
                        using (MemoryStream output = new MemoryStream()) {
                            SecurityExtension.DecryptAES(input, output, Unprotect(_token));
                            output.Position = 0;

                            Ressources           = JsonConvert.DeserializeObject <RessourceTableView>(Encoding.UTF8.GetString(output.ToArray()));
                            _ressourcesRetrieved = true;
                            return;
                        }
            } catch (Exception e) {
                ClientContext.Logger.LogError(e);
            }

            RetrieveRessourceTable(++counter);
        }
Beispiel #2
0
        static void AppendEncrypt(string token, string source, string destination)
        {
            RessourceTableView tableView = null;

            using (Stream input = File.OpenRead(Path.Combine(destination, "0000000000000000-1")))
                using (MemoryStream output = new MemoryStream()) {
                    SecurityExtension.DecryptAES(input, output, token);
                    output.Position = 0;
                    tableView       = JsonConvert.DeserializeObject <RessourceTableView>(Encoding.UTF8.GetString(output.ToArray()));
                }

            int    counter   = tableView.Table.Values.Max(x => x.Container) - 1;
            Stream container = null;

            foreach (string path in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories))
            {
                string encryptedPath = ByteArrayToString(token.Hash(Path.GetRelativePath(source, path).Replace(@"\", "/")));
                if (tableView.Table.ContainsKey(encryptedPath))
                {
                    continue;
                }
                Console.WriteLine("{0}  --  {1}", Path.GetRelativePath(source, path).Replace(@"\", "/"), encryptedPath);

                using (MemoryStream output = new MemoryStream())
                    using (Stream input = FileCompressed(path)) { //File.Open(path, FileMode.Open, FileAccess.Read)) {
                        SecurityExtension.EncryptAES(input, output, token);

                        if (container == null || container.Length + output.Length > (50 * 1024 * 1024))
                        {
                            container?.Dispose();
                            container = File.Open(Path.Combine(destination, $"0000000000000000-{++counter}"), FileMode.OpenOrCreate);
                            container.Seek(0, SeekOrigin.End); // seek end

                            Console.WriteLine("Current Archive: {0}", counter);
                        }

                        output.Position = 0;
                        tableView.Table.Add(encryptedPath, new RessourceTableItemView {
                            Container = counter,
                            Length    = output.Length,
                            Offset    = container.Position
                        });

                        output.CopyTo(container);
                    }
            }

            container?.Dispose();

            using (Stream output = File.Create(Path.Combine(destination, "0000000000000000-1")))
                using (MemoryStream input = new MemoryStream(
                           Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(tableView)))) {
                    SecurityExtension.EncryptAES(input, output, token);
                }
        }
Beispiel #3
0
        static void Encrypt(string token, string source, string destination)
        {
            RessourceTableView tableView = new RessourceTableView {
                Table     = new Dictionary <string, RessourceTableItemView>(),
                TokenHash = token.Hash()
            };

            int    counter   = 1;
            Stream container = null;

            foreach (string path in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories))
            {
                string encryptedPath = ByteArrayToString(token.Hash(Path.GetRelativePath(source, path).Replace(@"\", "/")));
                Console.WriteLine("{0}  --  {1}", Path.GetRelativePath(source, path).Replace(@"\", "/"), encryptedPath);

                using (MemoryStream output = new MemoryStream())
                    using (Stream input = FileCompressed(path)) { //File.Open(path, FileMode.Open, FileAccess.Read)) {
                        SecurityExtension.EncryptAES(input, output, token);

                        if (container == null || container.Length + output.Length > (50 * 1024 * 1024))
                        {
                            container?.Dispose();
                            container = File.Create(Path.Combine(destination, $"0000000000000000-{++counter}"));
                            Console.WriteLine("Current Archive: {0}", counter);
                        }

                        output.Position = 0;
                        tableView.Table.Add(encryptedPath, new RessourceTableItemView {
                            Container = counter,
                            Length    = output.Length,
                            Offset    = container.Position
                        });

                        output.CopyTo(container);
                    }
            }

            container?.Dispose();

            using (Stream output = File.Create(Path.Combine(destination, "0000000000000000-1")))
                using (MemoryStream input = new MemoryStream(
                           Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(tableView)))) {
                    SecurityExtension.EncryptAES(input, output, token);
                }

            using (Stream output = File.Create(Path.Combine(destination, "0000000000000000-0")))
                using (MemoryStream input = new MemoryStream(token.Hash())) {
                    input.CopyTo(output);
                }
        }