Is BouncyCastle's SecureRandom in C# threadsafe?

陌路散爱 提交于 2019-12-12 16:26:49

问题


The answer is apparently yes for the implementation in Java, but how about Org.BouncyCastle.Security.SecureRandom in C#?


回答1:


Since, as far as I know, there is no official (or even any) documentation of C# Bouncy Castle port - all we can do is look at source code and try to draw some conclusions. Here is source code of SecureRandom. We can see that the main methods there are NextCounterValue (used to generate seeds) and NextBytes used to generate actual random data. NextCounterValue is thread-safe (uses Interlocked.Increment). NextBytes forwards implementation to instance of IRandomGenerator. Since you can pass any instance of IRandomGenerator to constructor of SecureRandom - we can conclude that its thread safety depends on that of IRandomGenerator used.

Also when on full .NET Framework, SecureRandom uses CryptoApiRandomGenerator as master generator (to generate seeds) and that one is just wrapper around .NET RNGCryptoServiceProvider which as we know is thread safe.

What if you just create SecureRandom without passing any IRandomGenerator? Then it will create instance of DigestRandomGenerator (code) which seems to be thread-safe (uses simple lock in NextBytes).

All in all we can say that SecureRandom is thread safe if you are not passing an instance of IRandomGenerator which is not thread-safe to it.



来源:https://stackoverflow.com/questions/46792243/is-bouncycastles-securerandom-in-c-sharp-threadsafe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!