Skip to content

Commit

Permalink
Renamed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SommerEngineering committed Jan 5, 2020
1 parent 9640c75 commit 36001d0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
48 changes: 24 additions & 24 deletions Encrypter Tests/EncrypterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public async Task TestSimpleEnAndDecryption()
var message = "This is a test with umlauts äüö.";
var password = "test password";

var encryptedData = await CryptoProcessor.EncryptString(message, password);
var encryptedData = await CryptoProcessor.Encrypt(message, password);
Assert.That(encryptedData.Length, Is.AtLeast(message.Length)); // Note: Encrypted data contains salt as well!

var decryptedMessage = await CryptoProcessor.DecryptString(encryptedData, password);
var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password);
Assert.That(decryptedMessage, Is.EqualTo(message));
}

Expand All @@ -32,8 +32,8 @@ public async Task TestEmptyMessage()
var message = string.Empty;
var password = "test password";

var encryptedData = await CryptoProcessor.EncryptString(message, password);
var decryptedMessage = await CryptoProcessor.DecryptString(encryptedData, password);
var encryptedData = await CryptoProcessor.Encrypt(message, password);
var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password);
Assert.That(decryptedMessage, Is.EqualTo(message));
}

Expand All @@ -45,7 +45,7 @@ public async Task TestNoMessage()

try
{
var encryptedData = await CryptoProcessor.EncryptString(message, password);
var encryptedData = await CryptoProcessor.Encrypt(message, password);
Assert.Fail("Should not be reached!");
}
catch(CryptographicException e)
Expand All @@ -62,7 +62,7 @@ public async Task TestTooShortPassword4Encryption()

try
{
var encryptedData = await CryptoProcessor.EncryptString(message, password);
var encryptedData = await CryptoProcessor.Encrypt(message, password);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
Expand All @@ -77,11 +77,11 @@ public async Task TestTooShortPassword4Decryption()
var message = "This is a test with umlauts äüö.";
var password = "test password";

var encryptedData = await CryptoProcessor.EncryptString(message, password);
var encryptedData = await CryptoProcessor.Encrypt(message, password);

try
{
var decryptedMessage = await CryptoProcessor.DecryptString(encryptedData, password[..4]);
var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password[..4]);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
Expand All @@ -96,8 +96,8 @@ public async Task TestSimpleEnAndDecryptionWithASCII()
var message = Encoding.ASCII.GetString(Encoding.Convert(Encoding.UTF8, Encoding.ASCII, Encoding.UTF8.GetBytes("This is a test without umlauts.")));
var password = "test password";

var encryptedData = await CryptoProcessor.EncryptString(message, password);
var decryptedMessage = await CryptoProcessor.DecryptString(encryptedData, password);
var encryptedData = await CryptoProcessor.Encrypt(message, password);
var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password);
Assert.That(decryptedMessage, Is.EqualTo(message));
}

Expand All @@ -108,11 +108,11 @@ public async Task TestAlteredPassword()
var password1 = "password!";
var password2 = "password.";

var encryptedData = await CryptoProcessor.EncryptString(message, password1);
var encryptedData = await CryptoProcessor.Encrypt(message, password1);

try
{
var decryptedMessage = await CryptoProcessor.DecryptString(encryptedData, password2);
var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password2);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
Expand Down Expand Up @@ -140,16 +140,16 @@ public async Task TestUpgradedIterationsBehaviour()
var previousIterations = 1_000;
var upgradedIterations = 1_000_000;

var previousEncryptedData = await CryptoProcessor.EncryptString(message, password, previousIterations);
var previousEncryptedData = await CryptoProcessor.Encrypt(message, password, previousIterations);
var reEncryptedData = await CryptoProcessor.UpgradeIterations(previousEncryptedData, password, previousIterations, upgradedIterations);
Assert.That(previousEncryptedData, Is.Not.EqualTo(reEncryptedData));

var decryptedMessage = await CryptoProcessor.DecryptString(reEncryptedData, password, upgradedIterations);
var decryptedMessage = await CryptoProcessor.Decrypt(reEncryptedData, password, upgradedIterations);
Assert.That(decryptedMessage, Is.EqualTo(message));

try
{
var decryptedMessage2 = await CryptoProcessor.DecryptString(reEncryptedData, password, previousIterations);
var decryptedMessage2 = await CryptoProcessor.Decrypt(reEncryptedData, password, previousIterations);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
Expand All @@ -159,7 +159,7 @@ public async Task TestUpgradedIterationsBehaviour()

try
{
var decryptedMessage2 = await CryptoProcessor.DecryptString(previousEncryptedData, password, upgradedIterations);
var decryptedMessage2 = await CryptoProcessor.Decrypt(previousEncryptedData, password, upgradedIterations);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
Expand All @@ -176,16 +176,16 @@ public async Task TestChangedPasswordBehaviour()
var newPassword = "test password!!!";
var iterations = 1_000;

var previousEncryptedData = await CryptoProcessor.EncryptString(message, previousPassword, iterations);
var previousEncryptedData = await CryptoProcessor.Encrypt(message, previousPassword, iterations);
var reEncryptedData = await CryptoProcessor.ChangePassword(previousEncryptedData, previousPassword, newPassword, iterations);
Assert.That(previousEncryptedData, Is.Not.EqualTo(reEncryptedData));

var decryptedMessage = await CryptoProcessor.DecryptString(reEncryptedData, newPassword, iterations);
var decryptedMessage = await CryptoProcessor.Decrypt(reEncryptedData, newPassword, iterations);
Assert.That(decryptedMessage, Is.EqualTo(message));

try
{
var decryptedMessage2 = await CryptoProcessor.DecryptString(reEncryptedData, previousPassword, iterations);
var decryptedMessage2 = await CryptoProcessor.Decrypt(reEncryptedData, previousPassword, iterations);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
Expand All @@ -195,7 +195,7 @@ public async Task TestChangedPasswordBehaviour()

try
{
var decryptedMessage2 = await CryptoProcessor.DecryptString(previousEncryptedData, newPassword, iterations);
var decryptedMessage2 = await CryptoProcessor.Decrypt(previousEncryptedData, newPassword, iterations);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
Expand All @@ -216,8 +216,8 @@ public async Task TestSimpleStream()
try
{
await File.WriteAllTextAsync(tempSourceFile, message);
await CryptoProcessor.EncryptStream(File.OpenRead(tempSourceFile), File.OpenWrite(tempDestFile), password);
await CryptoProcessor.DecryptStream(File.OpenRead(tempDestFile), File.OpenWrite(tempFinalFile), password);
await CryptoProcessor.Encrypt(File.OpenRead(tempSourceFile), File.OpenWrite(tempDestFile), password);
await CryptoProcessor.Decrypt(File.OpenRead(tempDestFile), File.OpenWrite(tempFinalFile), password);

Assert.That(File.Exists(tempDestFile), Is.True);
Assert.That(File.Exists(tempFinalFile), Is.True);
Expand Down Expand Up @@ -277,8 +277,8 @@ public async Task Test32GBStream()
var fileInfoSource = new FileInfo(tempSourceFile);
Assert.That(fileInfoSource.Length, Is.EqualTo(32_000_000_000));

await CryptoProcessor.EncryptStream(File.OpenRead(tempSourceFile), File.OpenWrite(tempDestFile), password);
await CryptoProcessor.DecryptStream(File.OpenRead(tempDestFile), File.OpenWrite(tempFinalFile), password);
await CryptoProcessor.Encrypt(File.OpenRead(tempSourceFile), File.OpenWrite(tempDestFile), password);
await CryptoProcessor.Decrypt(File.OpenRead(tempDestFile), File.OpenWrite(tempFinalFile), password);

Assert.That(File.Exists(tempDestFile), Is.True);
Assert.That(File.Exists(tempFinalFile), Is.True);
Expand Down
16 changes: 8 additions & 8 deletions Encrypter/CryptoProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class CryptoProcessor
/// <param name="password">The password. Must consists of 6 chars or more.</param>
/// <param name="iterations">The number of iterations to derive the key. Should not be adjusted. The default is secure for the current time.</param>
/// <returns>The base64 encoded and encrypted string. The string is ASCII encoding.</returns>
public static async Task<string> EncryptString(string data, string password, int iterations = ITERATIONS_YEAR_2020)
public static async Task<string> Encrypt(string data, string password, int iterations = ITERATIONS_YEAR_2020)
{
if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
throw new CryptographicException("The password was empty or shorter than 6 characters.");
Expand Down Expand Up @@ -101,7 +101,7 @@ await Task.Run(() =>
/// <param name="outputStream">The desired output stream. The encrypted data gets written to the current position.</param>
/// <param name="password">The encryption password.</param>
/// <param name="iterations">The desired number of iterations to create the key. Should not be adjusted. The default is secure for the current time.</param>
public static async Task EncryptStream(Stream inputStream, Stream outputStream, string password, int iterations = ITERATIONS_YEAR_2020)
public static async Task Encrypt(Stream inputStream, Stream outputStream, string password, int iterations = ITERATIONS_YEAR_2020)
{
if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
throw new CryptographicException("The password was empty or shorter than 6 characters.");
Expand Down Expand Up @@ -171,7 +171,7 @@ await Task.Run(() =>
/// <param name="password">The password. Must consists of 6 chars or more.</param>
/// <param name="iterations">The number of iterations to derive the key. Should not be adjusted. The default is secure for the current time.</param>
/// <returns>The decrypted UTF8 encoded string.</returns>
public static async Task<string> DecryptString(string base64EncodedAndEncryptedData, string password, int iterations = ITERATIONS_YEAR_2020)
public static async Task<string> Decrypt(string base64EncodedAndEncryptedData, string password, int iterations = ITERATIONS_YEAR_2020)
{
if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
throw new CryptographicException("The password was empty or shorter than 6 characters.");
Expand Down Expand Up @@ -245,7 +245,7 @@ await Task.Run(() =>
/// <param name="outputStream">The desired output stream. The decrypted data gets written to the current position.</param>
/// <param name="password">The encryption password.</param>
/// <param name="iterations">The desired number of iterations to create the key. Should not be adjusted. The default is secure for the current time.</param>
public static async Task DecryptStream(Stream inputStream, Stream outputStream, string password, int iterations = ITERATIONS_YEAR_2020)
public static async Task Decrypt(Stream inputStream, Stream outputStream, string password, int iterations = ITERATIONS_YEAR_2020)
{
if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
throw new CryptographicException("The password was empty or shorter than 6 characters.");
Expand Down Expand Up @@ -313,10 +313,10 @@ await Task.Run(() =>
public static async Task<string> UpgradeIterations(string encryptedDataBeforeUpgrade, string password, int previousIterations, int upgradedIterations)
{
// Decrypt the data with the previous settings:
var decryptedData = await CryptoProcessor.DecryptString(encryptedDataBeforeUpgrade, password, previousIterations);
var decryptedData = await CryptoProcessor.Decrypt(encryptedDataBeforeUpgrade, password, previousIterations);

// Encrypt the data with the new settings:
return await CryptoProcessor.EncryptString(decryptedData, password, upgradedIterations);
return await CryptoProcessor.Encrypt(decryptedData, password, upgradedIterations);
}

/// <summary>
Expand All @@ -330,10 +330,10 @@ public static async Task<string> UpgradeIterations(string encryptedDataBeforeUpg
public static async Task<string> ChangePassword(string encryptedDataBeforeChange, string previousPassword, string newPassword, int iterations = ITERATIONS_YEAR_2020)
{
// Decrypt the data with the previous settings:
var decryptedData = await CryptoProcessor.DecryptString(encryptedDataBeforeChange, previousPassword, iterations);
var decryptedData = await CryptoProcessor.Decrypt(encryptedDataBeforeChange, previousPassword, iterations);

// Encrypt the data with the new settings:
return await CryptoProcessor.EncryptString(decryptedData, newPassword, iterations);
return await CryptoProcessor.Encrypt(decryptedData, newPassword, iterations);
}
}
}
8 changes: 4 additions & 4 deletions Encrypter/Encrypter.xml

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

8 changes: 4 additions & 4 deletions Encrypter/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class Extensions
/// <returns>The base64 encoded and encrypted string. The string is ASCII encoding.</returns>
public static async Task<string> Encrypt(this string data, string password, int iterations = CryptoProcessor.ITERATIONS_YEAR_2020)
{
return await CryptoProcessor.EncryptString(data, password, iterations);
return await CryptoProcessor.Encrypt(data, password, iterations);
}

/// <summary>
Expand All @@ -39,7 +39,7 @@ public static async Task<string> Encrypt(this string data, string password, int
/// <param name="iterations">The number of iterations to derive the key. Should not be adjusted. The default is secure for the current time.</param>
public static async Task Encrypt(this Stream inputStream, Stream outputStream, string password, int iterations = CryptoProcessor.ITERATIONS_YEAR_2020)
{
await CryptoProcessor.EncryptStream(inputStream, outputStream, password, iterations);
await CryptoProcessor.Encrypt(inputStream, outputStream, password, iterations);
}

/// <summary>
Expand All @@ -55,7 +55,7 @@ public static async Task Encrypt(this Stream inputStream, Stream outputStream, s
/// <returns>The decrypted UTF8 encoded string.</returns>
public static async Task<string> Decrypt(this string data, string password, int iterations = CryptoProcessor.ITERATIONS_YEAR_2020)
{
return await CryptoProcessor.DecryptString(data, password, iterations);
return await CryptoProcessor.Decrypt(data, password, iterations);
}

/// <summary>
Expand All @@ -73,7 +73,7 @@ public static async Task<string> Decrypt(this string data, string password, int
/// <param name="iterations">The number of iterations to derive the key. Should not be adjusted. The default is secure for the current time.</param>
public static async Task Decrypt(this Stream inputStream, Stream outputStream, string password, int iterations = CryptoProcessor.ITERATIONS_YEAR_2020)
{
await CryptoProcessor.DecryptStream(inputStream, outputStream, password, iterations);
await CryptoProcessor.Decrypt(inputStream, outputStream, password, iterations);
}
}
}

0 comments on commit 36001d0

Please sign in to comment.