This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.