Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Moving to RandomNumberGenerator as CryptRandom is not supported in Mono #857

Closed
wants to merge 1 commit into from

Conversation

harshgMSFT
Copy link
Contributor

@harshgMSFT
Copy link
Contributor Author

@GrabYourPitchforks , @Praburaj

@Praburaj
Copy link
Contributor

I verified that a private with this fix makes antiforgery support work on mono.

@@ -17,6 +18,7 @@ namespace Microsoft.AspNet.Mvc
[DebuggerDisplay("{DebuggerString}")]
internal sealed class BinaryBlob : IEquatable<BinaryBlob>
{
private static readonly RandomNumberGenerator _randomNumberGenerator = RandomNumberGenerator.Create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for caching the generator. Is there a significant cost to create the generator?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does mention that members aren't thread safe - http://msdn.microsoft.com/en-us/library/system.security.cryptography.randomnumbergenerator(v=vs.110).aspx. So invoking GetBytes concurrently might be bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well AFAIK RandomNumberGenerator.Create does some dictionary lookups inorder to figure out the default cryptographic random number generator ( and returns with RNGCryptoProvider instance), however actual perf would need to be measured. But as @pranavkm pointed out that GetBytes is not thread safe ( even though create is ) I think I will move out the cached and create a RandomNumberGenerator everytime a token is generated ( and cache it later if we see it to be the bottleneck).
(Unfortunately we don't have RNGCryptoProviderService in core clr, as that is thread safe).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All implementations of RandomNumberGenerator are required to be thread-safe, so it's safe to use a cached instance.

@yishaigalatzer
Copy link
Contributor

Please get signoff from @GrabYourPitchforks, then :shipit:

@GrabYourPitchforks
Copy link
Contributor

:shipit:

1 similar comment
@yishaigalatzer
Copy link
Contributor

:shipit:

@harshgMSFT harshgMSFT closed this Jul 24, 2014
@harshgMSFT harshgMSFT deleted the AntiForgeryFix branch July 24, 2014 21:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants