Reputation: 61
I am trying to found out what is the best way or library to generate secure random numbers such as secrets
in python.
I am working on windows 10 using codeblocks. I have done this so far:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(0));
int i;
i = (rand()%6)+1;
cout << i << "\n";
}
I am trying to replicate the same function as secrets
from python to generate secure random numbers.
Upvotes: 1
Views: 1196
Reputation: 73446
std::rand()
does not offer sufficient guarantees for being used in cryptographic applications (aka secure random number):
There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls).
You may obtain higher quality random generation using the <random>
library. But again, the generators used, such as for example the mersene twister is not sufficient for cryptographic use.
So you'd better go for specialized libraries such as OpenSSL.
Alternatively, given your platform choice, you can use the Windows API, with CryptGenRandom()
or, better, the newer BCryptGenRandom()
.
Upvotes: 5