113
main.c部分代码:
int main(){ void **pdh; pdh=(void **)malloc(20); int ret; ret = SDF_OpenDevice(pdh); if(ret != SDR_OK) { printf("打开设备失败\n"); } else { printf("打开设备成功!\n"); } printf("查看设备信息\n"); DEVICEINFO a; ret = SDF_GetDeviceInfo(*pdh,&a); if(ret !=SDR_OK) printf("查看设备信息失败!\n"); else printf("查看设备信息成功!\n"); printf("设备名字叫做%s\n",a.DeviceName); printf("设备版本号为%d\n",a.DeviceVersion); printf("想要获取的随机数长度(多少字节)为:\n"); int n; scanf("%d",&n); char string[100]; ret = SDF_GenerateRandom(*pdh,n*8,string); if(ret !=SDR_OK) printf("生成随机数失败!"); else printf("生成的随机数为%s\n",string);
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; }
编译运行结果: