Base58

編碼/解碼 Base-58 C++

  • February 22, 2022

我使用以下公式建構了一個 base-58 編碼器:https ://www.youtube.com/watch?v=GedV3S9X89c&feature=youtu.be

是否有更智能的方式使用 base-58 進行編碼/解碼?

我在某處讀到你需要一個我使用 Boost 的 bignum 庫。

我的目標是在這兩者之間進行轉換:

008D4D508F5BF2C28B20A3863405F05D3CD374B045E4B316E7
1Dt8ty59tU9LkrXG2ocWeSzKFAY8fu6jga

這個網站很容易做到: http: //lenschulwitz.com/base58

我知道比特幣原始碼有一個 base-58 編碼器/解碼器,但我不知道如何成功實現它。

我已經多次使用 Base58 編碼/解碼。相反,我討厭使用 BIGNUM 庫。所以,我使用了 Base-x的基本轉換算法。(確保尊重 MIT 許可證)它應該很容易翻譯成任何語言。

const char * const ALPHABET =
   "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const char ALPHABET_MAP[256] = {
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1,  0,  1,  2,  3,  4,  5,  6,  7,  8, -1, -1, -1, -1, -1, -1,
   -1,  9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
   22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
   -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
   47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};

const double iFactor = 1.36565823730976103695740418120764243208481439700722980119458355862779176747360903943915516885072037696111192757109;

// reslen is the allocated length for result, feel free to overallocate
int EncodeBase58(const unsigned char *source, int len, unsigned char result[], int reslen) {
   int zeros = 0, length = 0, pbegin = 0, pend;
   if (!(pend = len)) return 0;
   while (pbegin != pend && !source[pbegin]) pbegin = ++zeros;
   int size = 1 + iFactor * (double)(pend - pbegin);
   unsigned char b58[size];
   for (int i = 0; i < size; i++) b58[i] = 0;
   while (pbegin != pend) {
       unsigned int carry = source[pbegin];
       int i = 0;
       for (int it1 = size - 1; (carry || i < length) && (it1 != -1); it1--,i++) {
           carry += 256 * b58[it1];
           b58[it1] = carry % 58;
           carry /= 58;
       }
       if (carry) return 0;
       length = i;
       pbegin++;
   }
   int it2 = size - length;
   while ((it2-size) && !b58[it2]) it2++;
   if ((zeros + size - it2 + 1) > reslen) return 0;
   int ri = 0;
   while(ri < zeros) result[ri++] = '1';
   for (; it2 < size; ++it2) result[ri++] = ALPHABET[b58[it2]];
   result[ri] = 0;
   return ri;
}

// result must be declared (for the worst case): char result[len * 2];
int DecodeBase58(
   const unsigned char *str, int len, unsigned char *result) {
   result[0] = 0;
   int resultlen = 1;
   for (int i = 0; i < len; i++) {
       unsigned int carry = (unsigned int) ALPHABET_MAP[str[i]];
       if (carry == -1) return 0;
       for (int j = 0; j < resultlen; j++) {
           carry += (unsigned int) (result[j]) * 58;
           result[j] = (unsigned char) (carry & 0xff);
           carry >>= 8;
       }
       while (carry > 0) {
           result[resultlen++] = carry & 0xff;
           carry >>= 8;
       }
   }

   for (int i = 0; i < len && str[i] == '1'; i++)
       result[resultlen++] = 0;

   // Poorly coded, but guaranteed to work.
   for (int i = resultlen - 1, z = (resultlen >> 1) + (resultlen & 1);
       i >= z; i--) {
       int k = result[i];
       result[i] = result[resultlen - i - 1];
       result[resultlen - i - 1] = k;
   }
   return resultlen;
}

引用自:https://bitcoin.stackexchange.com/questions/76480