Skip to content

Commit

Permalink
Added iteration upgrading for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
SommerEngineering committed Jan 5, 2020
1 parent 36001d0 commit 83850f3
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
75 changes: 75 additions & 0 deletions Encrypter Tests/EncrypterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,81 @@ public async Task TestUpgradedIterationsBehaviour()
}
}

[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()
{
Expand Down
39 changes: 39 additions & 0 deletions Encrypter/CryptoProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,45 @@ public static async Task<string> UpgradeIterations(string encryptedDataBeforeUpg
return await CryptoProcessor.Encrypt(decryptedData, password, upgradedIterations);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="inputStreamBeforeUpgrade">The encrypted data with the previous settings.</param>
/// <param name="outputStreamUpgraded">The re-encrypted data.</param>
/// <param name="password">The password.</param>
/// <param name="previousIterations">The previous number of iterations.</param>
/// <param name="upgradedIterations">The upgraded number of iterations.</param>
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
{
}
}
}

/// <summary>
/// Changes the password of the encryption.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions Encrypter/Encrypter.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83850f3

Please sign in to comment.