Bitcoin source code Research (1)-BASE58 code

Source: Internet
Author: User
Tags alphanumeric characters

BASE58 encoding consists of 58 numbers and uppercase and lowercase letters, the bitcoin source code is defined and annotated as follows:


/** all alphanumeric characters except for "0", "I", "O", and "L " */

static const char* PSZBASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZABCDEFGHIJKMNOPQRSTUVWXYZ";


such as unsigned char ucdata[4] = {0x39, 0x3a, 0x3b, 0x3c}, the BASE58 encoding process is as follows:


1, first calculate ucdata start for 0x00 number zeros, here zeros = 0;

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

pbegin++;

zeroes++;

    }


2, skip the beginning of zeros 0x00, calculate the required cache

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


3, 256 The calculation of the binary turn 58 binary

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


Process the bytes.

while (pbegin! = pend) {

int carry = *pbegin;

int i = 0;

Apply "b58 = b58 * + ch".

For (std::vector<unsigned char>::reverse_iterator it = B58.rbegin ();

(Carry! = 0 | | I < length) && (It! = B58.rend ()); it++, i++) {

Carry + = * (*it);

*it = carry% 58;

Carry/= 58;

}


ASSERT (Carry = = 0);

length = i;

pbegin++;

}


4. Output encoding Result

Precede the string with zeros 1, followed by the prefix plus dstbyte corresponding pszBase58 character


This article from the "Clear" blog, reproduced please contact the author!

Bitcoin source code Research (1)-BASE58 code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.