AutoSeededRandomPool随机数发生器也是一个CryptoPP的无需外部输入种子的随机数发生器。它会利用系统提供的RNG算法来产生随机数。该算法的设计思想由 Leonard Janke.提出。
源代码如下:
#include<cryptlib.h>
#include<osrng.h>//包含AutoSeededRandomPool算法的头文件
#include<rijndael.h>//AES算法
#include<des.h>//DES算法
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
using namespace CryptoPP;
#define Array_Size 64
int main()
{
//定义一个AutoSeededRandomPool对象
AutoSeededRandomPool 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;
}
执行上述程序,运行结果如下图所示:
上面的显示结果是该随机数发生器产生1G bits数据的所花费的时间。
更多示例代码详见《深入浅出CryptoPP密码学库》随书电子文档:https://github.com/locomotive-crypto/crypto_book_1st