コード例 #1
0
        public void CorrectlyDecryptsAString()
        {
            var encrypted      = AES_256_CBC.Encrypt("TheValueToEncrypt", _password, _iv);
            var decryptedValue = AES_256_CBC.Decrypt(encrypted, _password);

            Assert.AreEqual(decryptedValue, "TheValueToEncrypt");
        }
コード例 #2
0
        private async Task RetrieveConfiguration()
        {
            var accessToken = await GetAccessToken();

            var authorizationHeader = Authorization.GenerateAuthorizationHeader(_config, _configurationPath, "get");

            var uri     = string.Format("{0}{1}", _config.Uri, _configurationPath);
            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method      = "GET";
            request.Accept      = "application/json";
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", authorizationHeader);
            request.Headers.Add("x-access-token", accessToken);
            request.Timeout = _config.RequestTimeout.HasValue ? _config.RequestTimeout.Value : Timeout;

            await Task.Run(async() =>
            {
                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            Stream responseStream = response.GetResponseStream();
                            var body = new StreamReader(responseStream).ReadToEnd();

                            var searchFor  = "\"configuration\":";
                            var startIndex = body.IndexOf(searchFor);
                            var endIndex   = body.IndexOf("],", startIndex + 1);
                            var originalEncryptedConfigurationBody = body.Substring(startIndex, (endIndex - startIndex) + 1);
                            var encryptedConfigurationBody         = originalEncryptedConfigurationBody.Substring(searchFor.Length).Trim();
                            var encryptedConfigurationBodyAsArray  = DeserializeObject <string[]>(encryptedConfigurationBody);
                            var decryptedConfigurationBody         = AES_256_CBC.Decrypt(encryptedConfigurationBodyAsArray, _config.ConfigEncryptionKey);
                            body = body.Replace(originalEncryptedConfigurationBody, searchFor + decryptedConfigurationBody);

                            var configurationResponse = DeserializeObject <ConfigurationResponse>(body);
                            var currentConfiguration  = new Dictionary <string, string>();

                            for (var i = 0; i < configurationResponse.configuration.Length; i++)
                            {
                                var item = configurationResponse.configuration[i];

                                if (!item.encrypted)
                                {
                                    currentConfiguration.Add(item.key, item.value[0]);
                                    continue;
                                }

                                item.value = new string[] { AES_256_CBC.Decrypt(item.value, _config.ConfigEncryptionKey) };
                                currentConfiguration.Add(item.key, item.value[0]);
                            }

                            _currentAppVersion      = configurationResponse.appVersion;
                            _currentEnvironment     = configurationResponse.environment;
                            _currentConfigurationId = configurationResponse.configurationId;
                            _currentVersionHash     = configurationResponse.configurationHash;
                            _currentConfiguration   = currentConfiguration;
                            _initialized            = true;

                            if (_config.Retrieved != null)
                            {
                                await Task.Run(_config.Retrieved);
                            }
                            return;
                        }

                        throw new Exception(string.Format("HttpStatusCode: {0} was returned", response.StatusCode));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("An error occurred while trying to refresh the configuration", ex);
                }
            });
        }