Added iteration upgrading for streams
This commit is contained in:
parent
36001d0f70
commit
83850f318e
@ -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]
|
[Test]
|
||||||
public async Task TestChangedPasswordBehaviour()
|
public async Task TestChangedPasswordBehaviour()
|
||||||
{
|
{
|
||||||
|
@ -319,6 +319,45 @@ namespace Encrypter
|
|||||||
return await CryptoProcessor.Encrypt(decryptedData, password, upgradedIterations);
|
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>
|
/// <summary>
|
||||||
/// Changes the password of the encryption.
|
/// Changes the password of the encryption.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -75,6 +75,17 @@
|
|||||||
<param name="upgradedIterations">The upgraded number of iterations.</param>
|
<param name="upgradedIterations">The upgraded number of iterations.</param>
|
||||||
<returns>The re-encrypted data.</returns>
|
<returns>The re-encrypted data.</returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:Encrypter.CryptoProcessor.UpgradeIterations(System.IO.Stream,System.IO.Stream,System.String,System.Int32,System.Int32)">
|
||||||
|
<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>
|
||||||
|
</member>
|
||||||
<member name="M:Encrypter.CryptoProcessor.ChangePassword(System.String,System.String,System.String,System.Int32)">
|
<member name="M:Encrypter.CryptoProcessor.ChangePassword(System.String,System.String,System.String,System.Int32)">
|
||||||
<summary>
|
<summary>
|
||||||
Changes the password of the encryption.
|
Changes the password of the encryption.
|
||||||
|
Loading…
Reference in New Issue
Block a user