Ejemplo n.º 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 EncryptFileTest()
        {
            // 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);

            // assert
            Assert.IsTrue(File.Exists(encryptedFilePath));
        }
        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");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Will save the settings derived from the commandline-arguments into a file. This facilitates automatic creation and utilization of transfer-settings.
        /// </summary>
        /// <param name="settingsParser"></param>
        /// <param name="settingsFilePath"></param>
        /// <param name="logger"></param>
        /// <param name="encryptionKeyFilePath">Optional. If specified, an encryption-key will be generated and stored to this file, and will be used to encrypt the settings file.</param>
        public static void SaveSettingsToFile(SettingsParser settingsParser, string settingsFilePath, ILogger logger, string encryptionKeyFilePath = null)
        {
            logger.LogInformation($"Will save settings-file to '{settingsFilePath}...'");

            // First, remove the settings-filepath argument from the settings. We don't want to include this in the saved settings.
            settingsParser.ApplicationSettings.SettingsFilePath    = null;
            settingsParser.ApplicationSettings.SettingsKeyFilePath = null;

            // Now go ahead and serialize the settings
            string serializedSettings = Newtonsoft.Json.JsonConvert.SerializeObject(settingsParser, Formatting.Indented);

            if (!string.IsNullOrWhiteSpace(encryptionKeyFilePath))
            {
                logger.LogInformation($"Will encrypt settings file {settingsFilePath}");
                string encryptionKey = null;

                bool encryptionFileExists = File.Exists(encryptionKeyFilePath);
                if (encryptionFileExists)
                {
                    encryptionKey = File.ReadAllText(encryptionKeyFilePath);
                }
                else
                {
                    // no encryption file exists, generate a new encryption key, save it, then encrypt the settings.
                    logger.LogInformation($"Generating new encryption key...");
                    encryptionKey = Guid.NewGuid().ToString().Substring(0, 8);
                    logger.LogInformation($"Saving encryption key to '{encryptionKeyFilePath}'...");
                    File.WriteAllText(encryptionKeyFilePath, encryptionKey);
                }

                FileEncryptionUtility fileEncryptionUtility = new FileEncryptionUtility();
                fileEncryptionUtility.EncryptFile(serializedSettings, settingsFilePath, encryptionKey);
            }
            else
            {
                // Save the settings into an unencrypted file.
                File.WriteAllText(settingsFilePath, serializedSettings);
            }

            logger.LogInformation($"Saved settings-file to '{settingsFilePath}'");
        }