I have created a method to gather entropy for my application.
I've put this in a Task to provide an asynchronous operation for the caller.
I want to avoid a forever loop when collecting entropy from the sensors so I used a timeout which defaults to using RNGCryptoServiceProvider.GetBytes()
method.
public Task<byte[]> GetEntropy1(int length)
{
RNGCryptoServiceProvider entropyGen = new RNGCryptoServiceProvider();
byte[] entropyBuffer = new byte[2048];
try
{
semaphore.Wait();
Task t = Task.Run(() =>
{
/* gather entropy from sensors */
});
if (!t.Wait(5000))
{
entropyGen.GetBytes(entropyBuffer);
}
return entropyBuffer;
}
catch (Exception)
{
return entropyBuffer;
}
finally
{
this.semaphore.Release();
}
}
But I don't understand why when I return the byte array, I get a warning about implicit cast.
Cannot implicitly convert type 'byte[]' to 'System.Threading.Tasks.Task'
If I use an async
in the function definition, then that warning goes away.