Fastest implementation of a true random number generator in C#

前端 未结 8 1232
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 23:23

I was reading about Random.Next() that for \"cryptographically secure random number suitable for creating a random password\" MSDN suggests RNGCryptoServiceProvider Class

8条回答
  •  无人及你
    2020-12-08 23:38

    The rule of thumb when it comes to security and cryptography stuff:

    Never write your own.

    Go with the standard way of doing it and avoid dangerous optimizations.

    Edit to address the updated question:

    Use Random.Next when you need statistically random numbers not used in security sensitive code and RNGCryptoServiceProvider in security sensitive code. It's not as fast as Random.Next but it has acceptable performance. You should benchmark to see the actual difference. It usually doesn't make sense to sacrifice security for performance.

提交回复
热议问题