openssl生成随机数

#include <stdio.h>
#include <openssl/bn.h>

int main()
{
        BIGNUM *bn;

        bn = BN_new(); //生成一个BIGNUM结构

        int bits = 20;
        int top = -1;
        int bottom = 1;

        BN_rand(bn, bits, top, bottom); //生成指定bits的随机数

        char *a = BN_bn2hex(bn); //转化成16进制字符串

        puts(a);

        BN_free(bn); //释放BIGNUM结构

        return 0;
}

实验效果:

# ./a.out 
062FAF
原文地址:https://www.cnblogs.com/zhangxuechao/p/11709322.html