Skip to content

Commit

Permalink
Updates async calls
Browse files Browse the repository at this point in the history
- Added ConfigureAwait(false)
  • Loading branch information
bitbeans committed May 3, 2015
1 parent 97feffb commit 41edf20
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions StreamCryptor/Cryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ private static bool ValidateKeyPair(KeyPair keyPair)
}

#region Synchronous Implementation

/// <summary>
/// (Self)Encrypts a file with libsodium and protobuf-net.
/// </summary>
/// <param name="senderKeyPair">The senders keypair.</param>
/// <param name="inputFile">The input file.</param>
/// <param name="outputFolder">There the encrypted file will be stored, if this is null the input directory is used.</param>
/// <param name="fileExtension">Set a custom file extenstion: .whatever</param>
/// <param name="maskFileName">Replaces the filename with some random name.</param>
/// <returns>The name of the encrypted file.</returns>
Expand All @@ -131,10 +133,7 @@ public static string EncryptFileWithStream(KeyPair senderKeyPair, string inputFi
try
{
//Call the main method
var task = Task.Run(async () =>
{
return await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, senderKeyPair.PublicKey, inputFile, null, outputFolder, fileExtension, maskFileName);
});
var task = Task.Run(async () => await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, senderKeyPair.PublicKey, inputFile, null, outputFolder, fileExtension, maskFileName).ConfigureAwait(false));
return task.Result;
}
catch (AggregateException ex)
Expand All @@ -157,6 +156,7 @@ public static string EncryptFileWithStream(KeyPair senderKeyPair, string inputFi
/// <param name="senderKeyPair">The senders keypair.</param>
/// <param name="recipientPublicKey">A 32 byte public key.</param>
/// <param name="inputFile">The input file.</param>
/// <param name="outputFolder">There the encrypted file will be stored, if this is null the input directory is used.</param>
/// <param name="fileExtension">Set a custom file extenstion: .whatever</param>
/// <param name="maskFileName">Replaces the filename with some random name.</param>
/// <returns>The name of the encrypted file.</returns>
Expand All @@ -174,10 +174,7 @@ public static string EncryptFileWithStream(KeyPair senderKeyPair, byte[] recipie
try
{
//Call the main method
var task = Task.Run(async () =>
{
return await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, recipientPublicKey, inputFile, null, outputFolder, fileExtension, maskFileName);
});
var task = Task.Run(async () => await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, recipientPublicKey, inputFile, null, outputFolder, fileExtension, maskFileName).ConfigureAwait(false));
return task.Result;
}
catch (AggregateException ex)
Expand Down Expand Up @@ -214,10 +211,7 @@ public static string EncryptFileWithStream(byte[] senderPrivateKey, byte[] sende
try
{
//Call the main method
var task = Task.Run(async () =>
{
return await EncryptFileWithStreamAsync(senderPrivateKey, senderPublicKey, recipientPublicKey, inputFile, null, outputFolder, fileExtension, maskFileName);
});
var task = Task.Run(async () => await EncryptFileWithStreamAsync(senderPrivateKey, senderPublicKey, recipientPublicKey, inputFile, null, outputFolder, fileExtension, maskFileName).ConfigureAwait(false));
return task.Result;
}
catch (AggregateException ex)
Expand Down Expand Up @@ -261,10 +255,7 @@ public static string DecryptFileWithStream(KeyPair keyPair, string inputFile, st
try
{
//Call the main method
var task = Task.Run(async () =>
{
return await DecryptFileWithStreamAsync(keyPair.PrivateKey, inputFile, outputFolder, null, overWrite);
});
var task = Task.Run(async () => await DecryptFileWithStreamAsync(keyPair.PrivateKey, inputFile, outputFolder, null, overWrite).ConfigureAwait(false));
return task.Result;
}
catch (AggregateException ex)
Expand Down Expand Up @@ -303,10 +294,7 @@ public static string DecryptFileWithStream(byte[] recipientPrivateKey, string in
try
{
//Call the main method
var task = Task.Run(async () =>
{
return await DecryptFileWithStreamAsync(recipientPrivateKey, inputFile, outputFolder, null, overWrite);
});
var task = Task.Run(async () => await DecryptFileWithStreamAsync(recipientPrivateKey, inputFile, outputFolder, null, overWrite).ConfigureAwait(false));
return task.Result;
}
catch (AggregateException ex)
Expand Down Expand Up @@ -346,7 +334,7 @@ public static async Task<string> EncryptFileWithStreamAsync(KeyPair senderKeyPai
{
throw new ArgumentOutOfRangeException("senderKeyPair", "invalid keypair");
}
return await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, senderKeyPair.PublicKey, inputFile, encryptionProgress, outputFolder, fileExtension, maskFileName);
return await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, senderKeyPair.PublicKey, inputFile, encryptionProgress, outputFolder, fileExtension, maskFileName).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -370,7 +358,7 @@ public static async Task<string> EncryptFileWithStreamAsync(KeyPair senderKeyPai
{
throw new ArgumentOutOfRangeException("senderKeyPair", "invalid keypair");
}
return await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, recipientPublicKey, inputFile, encryptionProgress, outputFolder, fileExtension, maskFileName);
return await EncryptFileWithStreamAsync(senderKeyPair.PrivateKey, senderKeyPair.PublicKey, recipientPublicKey, inputFile, encryptionProgress, outputFolder, fileExtension, maskFileName).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -480,7 +468,7 @@ public static async Task<string> EncryptFileWithStreamAsync(byte[] senderPrivate
{
//start reading the unencrypted file in chunks of the given length: CHUNK_LENGTH
byte[] unencryptedChunk = new byte[CHUNK_LENGTH];
bytesRead = await fileStreamUnencrypted.ReadAsync(unencryptedChunk, 0, CHUNK_LENGTH);
bytesRead = await fileStreamUnencrypted.ReadAsync(unencryptedChunk, 0, CHUNK_LENGTH).ConfigureAwait(false);
//check if there is still some work
if (bytesRead != 0)
{
Expand Down Expand Up @@ -562,7 +550,7 @@ public static async Task<string> DecryptFileWithStreamAsync(KeyPair keyPair, str
{
throw new ArgumentOutOfRangeException("keypair", "invalid keypair");
}
return await DecryptFileWithStreamAsync(keyPair.PrivateKey, inputFile, outputFolder, decryptionProgress, overWrite);
return await DecryptFileWithStreamAsync(keyPair.PrivateKey, inputFile, outputFolder, decryptionProgress, overWrite).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -682,7 +670,7 @@ public static async Task<string> DecryptFileWithStreamAsync(byte[] recipientPriv
//check the current chunk checksum
encryptedFileChunk.ValidateChunkChecksum(ephemeralKey, CHUNK_CHECKSUM_LENGTH);
byte[] decrypted = SecretBox.Open(encryptedFileChunk.Chunk, chunkNonce, Utils.GetEphemeralEncryptionKey(ephemeralKey));
await fileStreamUnencrypted.WriteAsync(decrypted, 0, decrypted.Length);
await fileStreamUnencrypted.WriteAsync(decrypted, 0, decrypted.Length).ConfigureAwait(false);
overallBytesRead += (long)decrypted.Length;
chunkNumber++;
overallChunkLength += encryptedFileChunk.ChunkLength;
Expand Down Expand Up @@ -775,7 +763,7 @@ public static async Task<DecryptedFile> DecryptFileWithStreamAsync(KeyPair keyPa
{
throw new ArgumentOutOfRangeException("keypair", "invalid keypair");
}
return await DecryptFileWithStreamAsync(keyPair.PrivateKey, inputFile, decryptionProgress);
return await DecryptFileWithStreamAsync(keyPair.PrivateKey, inputFile, decryptionProgress).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -868,7 +856,7 @@ public static async Task<DecryptedFile> DecryptFileWithStreamAsync(byte[] recipi
//check the current chunk checksum
encryptedFileChunk.ValidateChunkChecksum(ephemeralKey, CHUNK_CHECKSUM_LENGTH);
byte[] decrypted = SecretBox.Open(encryptedFileChunk.Chunk, chunkNonce, Utils.GetEphemeralEncryptionKey(ephemeralKey));
await fileStreamUnencrypted.WriteAsync(decrypted, 0, decrypted.Length);
await fileStreamUnencrypted.WriteAsync(decrypted, 0, decrypted.Length).ConfigureAwait(false);
overallBytesRead += (long)decrypted.Length;
chunkNumber++;
overallChunkLength += encryptedFileChunk.ChunkLength;
Expand Down

0 comments on commit 41edf20

Please sign in to comment.