main.c部分代码
int main(){
void ** pdh;
pdh = (void **) malloc(20);
int ret;
/*ret = SDF_OpenDevice(pdh);
if(ret != SDR_OK){
printf("error!");
} else {
printf("device opened!\n");
}
DEVICEINFO testdi;
ret = SDF_GetDeviceInfo(pdh, &testdi);
if(ret != SDR_OK){
printf("error!");
} else {
printf("Issuer Name: %s\n", testdi.IssuerName);
printf("Device Name: %s\n", testdi.DeviceName);
printf("Device Serial: %s\n", testdi.DeviceSerial);
printf("Device Version: %d\n", testdi.DeviceVersion);
}*/
unsigned int n;
unsigned char pRandom[10017];
scanf("%d", &n);
ret = SDF_GenerateRandom(*pdh, n, pRandom);
if(ret != SDR_OK){
printf("error!");
} else {
for(int i=0; i<n; i++)
printf("%02x", pRandom[i]);
}
printf("\n");
ret = SDF_CloseDevice(*pdh);
/*if(ret != SDR_OK){
printf("error!");
} else {
free(pdh);
printf("device closed!\n");
}*/
return 0;
}
sdf.c代码
static int getRandom(char *r, int length)
{
BIGNUM *bn;
int i;
bn = BN_new(); //生成一个BIGNUM结构
//int bits = 20;
int top = -1;
int bottom = 1;
BN_rand(bn, length *8, top, bottom); //生成指定bits的随机数 uilength是bits数= length *8,而length是字节数 = uilengh/8
char *a = BN_bn2hex(bn); //转化成16进制字符串
//puts(a);
//printf("\n");
for(i=0;*(a+i)!='\0';i++)
{
*(r+i)=*(a+i);
}
*(r+i)='\0';
BN_free(bn); //释放BIGNUM结构
return 0;
}
int SDF_GenerateRandom (void * hSessionHandle,unsigned int uiLength,unsigned char * pucRandom)
{
int k=getRandom(pucRandom,uiLength/8);
if(k==0)
printf("生成随机数成功!");
return SDR_OK;
}
还要记得在头文件里加上#include <openssl/rand.h>
测试结果
可知正确