温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

BitCoin源码研究(1)-Base58编码

发布时间:2020-03-31 18:45:45 来源:网络 阅读:7838 作者:ggxxjj123 栏目:编程语言

Base58编码由58个数字和大小写字母组成,BitCoin源码中定义及注释如下:


/** All alphanumeric characters except for "0", "I", "O", and "l" */

static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";


如unsigned char ucData[4] = { 0x39, 0x3a, 0x3b, 0x3c };的base58编码过程如下:


1、先计算ucData开头为0x00的个数 zeros ,这里zeros = 0;

    while (pbegin != pend && *pbegin == 0) {

        pbegin++;

        zeroes++;

    }


2、跳过开头的zeros个0x00,计算所需要的缓存

    int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up.


3、256进制转58进制的计算

std::vector<unsigned char> b58(size);


// Process the bytes.

while (pbegin != pend) {

    int carry = *pbegin;

    int i = 0;

    // Apply "b58 = b58 * 256 + ch".

    for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); 

                    (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {

        carry += 256 * (*it);

        *it = carry % 58;

        carry /= 58;

    }


    assert(carry == 0);

    length = i;

    pbegin++;

}


4、输出编码结果

先在字符串前补上zeros 个1 ,后面的依次缀加DstByte对应的 pszBase58 字符


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI