• openssl随机数测试


    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>

    测试结果

    可知正确

  • 相关阅读:
    小程序中点击input控件键盘弹出时placeholder文字上移
    微服务学习记录-consul服务发现
    微服务学习记录-ocelot网关
    一些新了解到技术
    warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
    linux 7 创建DNS服务器
    ESXi 主机创建datastore失败
    检查MD5
    为戴尔服务器下载ESXi
    vCenter Server上的报警消除
  • 原文地址:https://www.cnblogs.com/20191211yss/p/16202787.html
Copyright © 2020-2023  润新知