public IReadOnlyCollection <Valuable> DecryptValuablesCiphertext ( FindValuablesProjection projection ) { var virgilCrypto = new VirgilCrypto(); SerilogLogger.Instance.Debug ( "Generating the key hash. AppId='{AppId}'", _appId ); var appKey = Convert.ToBase64String ( virgilCrypto.GenerateHash ( Encoding.UTF8.GetBytes ( _appPassword ), HashAlgorithm.SHA512 ) ); SerilogLogger.Instance.Debug ( "Importing the private key. AppId='{AppId}'", _appId ); var privateKey = virgilCrypto.ImportPrivateKey ( Convert.FromBase64String(projection.PrivateKeyCiphertext), appKey ); SerilogLogger.Instance.Debug ( "Decrypting the app settings. AppId='{AppId}'", _appId ); var decrypted = Encoding.UTF8.GetString ( virgilCrypto.Decrypt ( Convert.FromBase64String(projection.Ciphertext), privateKey ) ); var valuables = JsonConvert.DeserializeObject <IReadOnlyCollection <Valuable> > ( decrypted ); AddOption ( ConfigurKeys.SignalRAccessToken, projection.SignalR.AccessToken ); AddOption ( ConfigurKeys.SignalRUrl, projection.SignalR.Url ); return(valuables); }
private async Task LoadAsync() { Data = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); AddConfigurOptions(); FindValuablesProjection projection = null; string findValuablesResponseContent = null; var stopwatch = Stopwatch.StartNew(); try { SerilogLogger.Instance.Information ( "Attempting to find valuables from the API. AppId='{AppId}'", _appId ); findValuablesResponseContent = await FindValuablesAsync(); projection = JsonConvert.DeserializeObject <FindValuablesProjection> ( findValuablesResponseContent ); SerilogLogger.Instance.Information ( "Successfully found valuables from the API in {ElapsedMilliseconds}ms. AppId='{AppId}'", stopwatch.ElapsedMilliseconds, _appId ); } catch (Exception exception) { SerilogLogger.Instance.Error ( exception, "Failed to find valuables from the API in {ElapsedMilliseconds}ms. AppId='{AppId}'", stopwatch.ElapsedMilliseconds, _appId ); } finally { stopwatch.Stop(); } if (_configurOptions.IsFileCacheEnabled) { var fileCachePath = $"configur_find-valuables_{_appId}.json"; if (projection != null) { try { SerilogLogger.Instance.Information ( "Attempting to save valuables to the file cache. AppId='{AppId}'", _appId ); if (!File.Exists(fileCachePath)) { File.Create(fileCachePath) .Dispose(); } File.WriteAllText ( fileCachePath, findValuablesResponseContent, Encoding.UTF8 ); SerilogLogger.Instance.Information ( "Successfully saved valuables to the file cache. AppId='{AppId}'", _appId ); } catch (Exception exception) { SerilogLogger.Instance.Error ( exception, "Failed to save valuables to the file cache. AppId='{AppId}'", _appId ); } } else if (File.Exists(fileCachePath)) { try { SerilogLogger.Instance.Information ( "Attempting to load valuables from the file cache. AppId='{AppId}'", _appId ); var fileContents = File.ReadAllText ( fileCachePath, Encoding.UTF8 ); projection = JsonConvert.DeserializeObject <FindValuablesProjection> ( fileContents ); SerilogLogger.Instance.Information ( "Successfully loaded valuables from the file cache. AppId='{AppId}'", _appId ); } catch (Exception exception) { SerilogLogger.Instance.Error ( exception, "Failed to load valuables from the file cache. AppId='{AppId}'", _appId ); } } } if (projection == null) { SerilogLogger.Instance.Warning ( "Failed to add app settings. AppId='{AppId}'", _appId ); return; } IReadOnlyCollection <Valuable> valuables = null; try { valuables = DecryptValuablesCiphertext ( projection ); } catch (Exception exception) { SerilogLogger.Instance.Error ( exception, "Failed to decrypt valuables. AppId='{AppId}'", _appId ); } var valuableCount = 0; if (valuables != null) { foreach (var valuable in valuables) { var key = valuable.Key; AddOption ( key, valuable.Value ); valuableCount++; } } SerilogLogger.Instance.Information ( "Added " + valuableCount + " app setting(s). AppId='{AppId}'", _appId ); }