Skip to content

Commit

Permalink
Added password change for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
SommerEngineering committed Jan 5, 2020
1 parent 83850f3 commit 96f7832
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Encrypter Tests/EncrypterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,93 @@ public async Task TestChangedPasswordBehaviour()
}
}

[Test]
public async Task TestChangedPasswordBehaviourStreaming()
{
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 passwordPrevious = "test password";
var passwordNext = "better password";
var iterations = 1_000;

await using (var outputStream = File.OpenWrite(tempFileEncryptedPrevious))
{
await using var inputStream = File.OpenRead(tempFileInput);
await CryptoProcessor.Encrypt(inputStream, outputStream, passwordPrevious, iterations);
}

await using (var outputStream = File.OpenWrite(tempFileReEncrypted))
{
await using var inputStream = File.OpenRead(tempFileEncryptedPrevious);
await CryptoProcessor.ChangePassword(inputStream, outputStream, passwordPrevious, passwordNext, iterations);
}

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, passwordNext, iterations);
}

Assert.That(await File.ReadAllTextAsync(tempFileDecrypted), Is.EqualTo(message));

try
{
await using var tempBuffer = new MemoryStream();
await using var inputStream = File.OpenRead(tempFileReEncrypted);
await CryptoProcessor.Decrypt(inputStream, tempBuffer, passwordPrevious, iterations);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
{
Assert.That(true);
}
}
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 TestSimpleStream()
{
Expand Down
39 changes: 39 additions & 0 deletions Encrypter/CryptoProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,44 @@ public static async Task<string> ChangePassword(string encryptedDataBeforeChange
// Encrypt the data with the new settings:
return await CryptoProcessor.Encrypt(decryptedData, newPassword, iterations);
}

/// <summary>
/// Changes the password of the encryption. 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="encryptedInput">With the previous password encrypted data.</param>
/// <param name="reEncryptedOutput">The re-encrypted data.</param>
/// <param name="previousPassword">The previous password.</param>
/// <param name="newPassword">The new password.</param>
/// <param name="iterations">The used iterations.</param>
public static async Task ChangePassword(Stream encryptedInput, Stream reEncryptedOutput, string previousPassword, string newPassword, int iterations = ITERATIONS_YEAR_2020)
{
var tempFileCache = Path.GetTempFileName();

try
{
await using (var tempCacheStream = File.OpenWrite(tempFileCache))
{
// Decrypt the data with the previous settings:
await Decrypt(encryptedInput, tempCacheStream, previousPassword, iterations);
}

await using (var tempCacheStream = File.OpenRead(tempFileCache))
{
// Encrypt the data with the new settings:
await Encrypt(tempCacheStream, reEncryptedOutput, newPassword, iterations);
}
}
finally
{
try
{
File.Delete(tempFileCache);
}
catch
{
}
}
}
}
}
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 96f7832

Please sign in to comment.