mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 13:32:47 +01:00
b58be132c9
This removes the bignum/OpenSSL dependency. The base58 transformation code is also moved to a separate .cpp file.
92 lines
3.0 KiB
C++
92 lines
3.0 KiB
C++
// Copyright (c) 2014 The Bitcoin developers
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
/* All alphanumeric characters except for "0", "I", "O", and "l" */
|
|
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
|
|
bool DecodeBase58(const char *psz, std::vector<unsigned char>& vch) {
|
|
// Skip leading spaces.
|
|
while (*psz && isspace(*psz))
|
|
psz++;
|
|
// Skip and count leading '1's.
|
|
int zeroes = 0;
|
|
while (*psz == '1') {
|
|
zeroes++;
|
|
psz++;
|
|
}
|
|
// Allocate enough space in big-endian base256 representation.
|
|
std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up.
|
|
// Process the characters.
|
|
while (*psz && !isspace(*psz)) {
|
|
// Decode base58 character
|
|
const char *ch = strchr(pszBase58, *psz);
|
|
if (ch == NULL)
|
|
return false;
|
|
// Apply "b256 = b256 * 58 + ch".
|
|
int carry = ch - pszBase58;
|
|
for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) {
|
|
carry += 58 * (*it);
|
|
*it = carry % 256;
|
|
carry /= 256;
|
|
}
|
|
assert(carry == 0);
|
|
psz++;
|
|
}
|
|
// Skip trailing spaces.
|
|
while (isspace(*psz))
|
|
psz++;
|
|
if (*psz != 0)
|
|
return false;
|
|
// Skip leading zeroes in b256.
|
|
std::vector<unsigned char>::iterator it = b256.begin();
|
|
while (it != b256.end() && *it == 0)
|
|
it++;
|
|
// Copy result into output vector.
|
|
vch.reserve(zeroes + (b256.end() - it));
|
|
vch.assign(zeroes, 0x00);
|
|
while (it != b256.end())
|
|
vch.push_back(*(it++));
|
|
return true;
|
|
}
|
|
|
|
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) {
|
|
// Skip & count leading zeroes.
|
|
int zeroes = 0;
|
|
while (pbegin != pend && *pbegin == 0) {
|
|
pbegin++;
|
|
zeroes++;
|
|
}
|
|
// Allocate enough space in big-endian base58 representation.
|
|
std::vector<unsigned char> b58((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up.
|
|
// Process the bytes.
|
|
while (pbegin != pend) {
|
|
int carry = *pbegin;
|
|
// Apply "b58 = b58 * 256 + ch".
|
|
for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) {
|
|
carry += 256 * (*it);
|
|
*it = carry % 58;
|
|
carry /= 58;
|
|
}
|
|
assert(carry == 0);
|
|
pbegin++;
|
|
}
|
|
// Skip leading zeroes in base58 result.
|
|
std::vector<unsigned char>::iterator it = b58.begin();
|
|
while (it != b58.end() && *it == 0)
|
|
it++;
|
|
// Translate the result into a string.
|
|
std::string str;
|
|
str.reserve(zeroes + (b58.end() - it));
|
|
str.assign(zeroes, '1');
|
|
while (it != b58.end())
|
|
str += pszBase58[*(it++)];
|
|
return str;
|
|
}
|