和前面提到的RandomPool算法一样,NonblockingRng算法也不需要外部输入种子。
RandomPool算法:https://blog.csdn.net/lunar_queen/article/details/81560777。该算法包含在CryptoPP的osrng.h头文件中。
源代码如下:
#include<cryptlib.h>
#include<osrng.h>//包含NonblockingRng算法的头文件
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
using namespace CryptoPP;
#define Array_Size 64
int main()
{
//定义一个NonblockingRng对象
NonblockingRng rng;
//定义一个文件对象
ofstream file("data.dat",ios_base::binary | ios_base::out);
byte output[Array_Size];
//产生1Gbits的数据。每调用一次随机数发生器,产生8*64=512bits的数据。
//1Gbits = 1000*1000*1000。
//1000*1000*1000/512 = 1953125。
cout << "开始生成数据..." << endl;
clock_t start = clock();
for(int i=0; i < 1953125 ; ++i)
{
rng.GenerateBlock(output,Array_Size);
file.write(reinterpret_cast<const char*>(output),Array_Size);
}
clock_t end = clock();
cout << "数据生成完毕..." << endl;
double duration;
duration = (double)(end - start) / CLOCKS_PER_SEC;
cout << "生成数据总共耗时:" << duration << endl;
file.close();
return 0;
}
执行上述程序,运行结果如下图所示:
更多示例代码详见《深入浅出CryptoPP密码学库》随书电子文档:https://github.com/locomotive-crypto/crypto_book_1st