• Openssl 学习—2.BIGNUM结构


    Openssl 学习—2.BIGNUM结构

    BIGNUM数据结构是openssl中带的专门处理大数的一种结构

    一、结构简介

    先看什么是BIGNUM:

    typedef struct bignum_st BIGNUM;

    struct bignum_st

    {

    BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */

    int top; /* Index of last used d +1. */

    /* The next are internal book keeping for bn_expand. */

    int dmax; /* Size of the d array. */

    int neg; /* one if the number is negative */

    int flags;

    };

    top: 使用的d结构的数目

    dmax:d数组的容量

    neg:是否为负数

    flags:/

    d:d是什么、占多少字节从下方获得:

    /* assuming long is 64bit */

    #ifdef SIXTY_FOUR_BIT_LONG

    #define BN_ULONG unsigned long

    ……

    #endif

     

    /* This is where the long long data type is 64 bits, but long is 32.

    #ifdef SIXTY_FOUR_BIT

    #define BN_ULONG unsigned long long

    ……

    #endif

     

    #ifdef THIRTY_TWO_BIT

    #define BN_ULONG unsigned int

    ……

    #endif

    本机为32位,即THIRTY_TWO_BITBN_ULONG定义为 unsigned int32

    二、可进行的操作

    1. 创建与释放

    BIGNUM * BN_new (void);

    创建一个BIGNUM的结构,返回新BIGNUM结构的指针

    BIGNUM *a = BN_new ();

    void BN_free (BIGNUM *);

    释放一个BIGNUM

    free (a);

         

    2. 值测试

    int BN_cmp (BIGNUM *a,

    BIGNUM *b);

    判断a与b是否相等

    if (BN_cmp (a, b) { printf ("a equ b/n"); }

    int BN_is_zero(BIGNUM *a);

    判断a是不是为0

    if (BN_is_zero (a))

    int BN_is_one(BIGNUM *a);

    判断a是不是1

    if (BN_is_one (a))

    int BN_is_word(BIGNUM *a,

    BN_ULONG w);    

    判断a是不是值w

    if (BN_is_word (a, 12))

    int BN_is_odd(BIGNUM *a);

    判断a是不是一个奇数

    if (BN_is_odd (a))

         

    3. 操作

    Int BN_rand(BIGNUM *rnd, int bits, int top,int bottom);

       

    void    BN_init(BIGNUM *);

       

    BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);

       

    void    BN_swap(BIGNUM *a, BIGNUM *b);

       
         

    4. 赋值与取值

    int BN_num_bytes(BIGNUM *a);

    返回a的字节数

    printf ("length: %d/n", BN_num_tytes (a));

    int BN_num_bits(BIGNUM *a);

    返回a的二进制位数

    printf ("bits: %d/n", BN_num_bits (a));

    int BN_one(BIGNUM *a);

    设置a为1

    BN_one (a);

    int BN_zero(BIGNUM *a);

    设置a为0

    BN_zero (a);

    int BN_bn2bin(const BIGNUM *a, unsigned char *to);

    取a为二进制到to中,返回字符串长度

    char s[1024];
    int length = BN_bn2bin (a, s);

    BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);

    赋二进制值s到ret中,返回ret

    char s[] = "1001001";
    BN_bin2bn (s, strlen (s), a);

    char *BN_bn2hex(const BIGNUM *a);

    取a的16进制值,返回一个字符串的指针。此指针要使用完后,手动使用OPENSSL_free释放

    char *p = BN_bn2hex (a);
    if (p)
    {
    printf ("number is 0x%s/n", p);
    OPENSSL_free (p);
    }

    char *BN_bn2dec(const BIGNUM *a);

    取a的10进制值,返回一个字符串的指针。此指针要使用完后,手动使用OPENSSL_free释放

    p = BN_bn2dec (a);

    int BN_hex2bn(BIGNUM **a, const char *str);

    赋16进制值str到*a中,返回成功与否

    BN_hex2bn (&a, "0x123F23D12");

    int BN_dec2bn(BIGNUM **a, const char *str);

    赋10进制值str到*a中,返回成功与否

    BN_dec2bn (&a, "1999");

         

     

    http://blog.csdn.net/wjj1987/article/details/6445092

  • 相关阅读:
    CentOS_关机与重启命令详解
    去除ArrayList重复的对象
    单项设计模式
    死循环的应用
    java学习心得-面向对象与面向过程
    计算机使用个人经验及日常维护
    linux操作系统简介
    集合
    项目学习4
    周末总结
  • 原文地址:https://www.cnblogs.com/mutou3221/p/3132030.html
Copyright © 2020-2023  润新知