예제 #1
0
        public static SettingsParser ImportSettingsFromFile(string settingsFilePath, ILogger logger, string encryptionKeyFilePath = null)
        {
            logger.LogInformation($"Will read settings-file fom '{settingsFilePath}...'");

            // De-serialize the settings
            string deserializedSettingsText = null;

            if (string.IsNullOrWhiteSpace(encryptionKeyFilePath))
            {
                // No encryption-key file given, so we'll assume the settings-file is un-encrypted.
                deserializedSettingsText = File.ReadAllText(settingsFilePath);
            }
            else
            {
                // Encryption-key file was provided, so we'll assume the settings-file is encrypted and, well, decrypt it.
                logger.LogInformation($"Will decrypt settings file {settingsFilePath} using the key found in {encryptionKeyFilePath}");
                string encryptionKey = File.ReadAllText(encryptionKeyFilePath);

                FileEncryptionUtility fileEncryptionUtility = new FileEncryptionUtility();
                deserializedSettingsText = fileEncryptionUtility.DecryptFile(settingsFilePath, encryptionKey);
            }

            SettingsParser deserializedSettings = JsonConvert.DeserializeObject <SettingsParser>(deserializedSettingsText);

            // Remove the settings-filepath argument from the settings. We don't want to include this in the referenced settings, they're only used for this import-purpose.
            deserializedSettings.ApplicationSettings.SettingsFilePath    = null;
            deserializedSettings.ApplicationSettings.SettingsKeyFilePath = null;

            logger.LogInformation($"Imported settings from '{settingsFilePath}'");
            return(deserializedSettings);
        }
        public void DecryptFileTest()
        {
            // arrange
            FileEncryptionUtility fileEncryptionUtility = new FileEncryptionUtility();
            string stringToEncrypt   = "stringToEncrypt";
            string encryptedFilePath = Path.GetTempPath() + $"{DateTime.Now.Ticks.ToString()}";
            string encryptionKey     = Guid.NewGuid().ToString().Substring(0, 8);

            // act
            fileEncryptionUtility.EncryptFile(stringToEncrypt, encryptedFilePath, encryptionKey);
            string decryptedText = fileEncryptionUtility.DecryptFile(encryptedFilePath, encryptionKey);

            // assert
            Assert.AreEqual(decryptedText, "stringToEncrypt");
        }