diff --git a/Encrypter Tests/EncrypterTests.cs b/Encrypter Tests/EncrypterTests.cs index 451cffd..ddad93c 100644 --- a/Encrypter Tests/EncrypterTests.cs +++ b/Encrypter Tests/EncrypterTests.cs @@ -168,6 +168,81 @@ namespace Encrypter_Tests } } + [Test] + public async Task TestUpgradedIterationsBehaviourStreaming() + { + var tempFileInput = Path.GetTempFileName(); + var tempFileEncryptedPrevious = Path.GetTempFileName(); + var tempFileReEncrypted = Path.GetTempFileName(); + var tempFileDecrypted = Path.GetTempFileName(); + + try + { + var message = "This is a test with umlauts äüö."; + await File.WriteAllTextAsync(tempFileInput, message); + + var password = "test password"; + var previousIterations = 1_000; + var upgradedIterations = 1_000_000; + + await using (var outputStream = File.OpenWrite(tempFileEncryptedPrevious)) + { + await using var inputStream = File.OpenRead(tempFileInput); + await CryptoProcessor.Encrypt(inputStream, outputStream, password, previousIterations); + } + + await using (var outputStream = File.OpenWrite(tempFileReEncrypted)) + { + await using var inputStream = File.OpenRead(tempFileEncryptedPrevious); + await CryptoProcessor.UpgradeIterations(inputStream, outputStream, password, previousIterations, upgradedIterations); + } + + Assert.That(await File.ReadAllBytesAsync(tempFileEncryptedPrevious), Is.Not.EqualTo(await File.ReadAllBytesAsync(tempFileReEncrypted))); + + await using (var outputStream = File.OpenWrite(tempFileDecrypted)) + { + await using var inputStream = File.OpenRead(tempFileReEncrypted); + await CryptoProcessor.Decrypt(inputStream, outputStream, password, upgradedIterations); + } + + Assert.That(await File.ReadAllTextAsync(tempFileDecrypted), Is.EqualTo(message)); + } + finally + { + try + { + File.Delete(tempFileInput); + } + catch + { + } + + try + { + File.Delete(tempFileDecrypted); + } + catch + { + } + + try + { + File.Delete(tempFileEncryptedPrevious); + } + catch + { + } + + try + { + File.Delete(tempFileReEncrypted); + } + catch + { + } + } + } + [Test] public async Task TestChangedPasswordBehaviour() { diff --git a/Encrypter/CryptoProcessor.cs b/Encrypter/CryptoProcessor.cs index 3ed74d5..a9c9fb7 100644 --- a/Encrypter/CryptoProcessor.cs +++ b/Encrypter/CryptoProcessor.cs @@ -319,6 +319,45 @@ namespace Encrypter return await CryptoProcessor.Encrypt(decryptedData, password, upgradedIterations); } + /// + /// Upgrades the encryption regarding the used iterations for the key. In order to re-encrypt the stream, a temporary file + /// gets used. When the returned task is finished, the re-encryption is done as well. + /// + /// The encrypted data with the previous settings. + /// The re-encrypted data. + /// The password. + /// The previous number of iterations. + /// The upgraded number of iterations. + public static async Task UpgradeIterations(Stream inputStreamBeforeUpgrade, Stream outputStreamUpgraded, string password, int previousIterations, int upgradedIterations) + { + var tempFileCache = Path.GetTempFileName(); + + try + { + await using (var tempCacheStream = File.OpenWrite(tempFileCache)) + { + // Decrypt the data with the previous settings: + await Decrypt(inputStreamBeforeUpgrade, tempCacheStream, password, previousIterations); + } + + await using (var tempCacheStream = File.OpenRead(tempFileCache)) + { + // Encrypt the data with the new settings: + await Encrypt(tempCacheStream, outputStreamUpgraded, password, upgradedIterations); + } + } + finally + { + try + { + File.Delete(tempFileCache); + } + catch + { + } + } + } + /// /// Changes the password of the encryption. /// diff --git a/Encrypter/Encrypter.xml b/Encrypter/Encrypter.xml index efb560e..aaf394e 100644 --- a/Encrypter/Encrypter.xml +++ b/Encrypter/Encrypter.xml @@ -75,6 +75,17 @@ The upgraded number of iterations. The re-encrypted data. + + + Upgrades the encryption regarding the used iterations for the key. In order to re-encrypt the stream, a temporary file + gets used. When the returned task is finished, the re-encryption is done as well. + + The encrypted data with the previous settings. + The re-encrypted data. + The password. + The previous number of iterations. + The upgraded number of iterations. + Changes the password of the encryption.