Changeset View
Changeset View
Standalone View
Standalone View
src/base58.cpp
// Copyright (c) 2014-2016 The Bitcoin Core developers | // Copyright (c) 2014-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <base58.h> | #include <base58.h> | ||||
#include <hash.h> | #include <hash.h> | ||||
#include <uint256.h> | #include <uint256.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/string.h> | |||||
#include <cassert> | #include <cassert> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <cstring> | #include <cstring> | ||||
#include <limits> | #include <limits> | ||||
/** All alphanumeric characters except for "0", "I", "O", and "l" */ | /** All alphanumeric characters except for "0", "I", "O", and "l" */ | ||||
static const char *pszBase58 = | static const char *pszBase58 = | ||||
▲ Show 20 Lines • Show All 122 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
std::string EncodeBase58(const std::vector<uint8_t> &vch) { | std::string EncodeBase58(const std::vector<uint8_t> &vch) { | ||||
return EncodeBase58(vch.data(), vch.data() + vch.size()); | return EncodeBase58(vch.data(), vch.data() + vch.size()); | ||||
} | } | ||||
bool DecodeBase58(const std::string &str, std::vector<uint8_t> &vchRet, | bool DecodeBase58(const std::string &str, std::vector<uint8_t> &vchRet, | ||||
int max_ret_len) { | int max_ret_len) { | ||||
if (!ValidAsCString(str)) { | |||||
return false; | |||||
} | |||||
return DecodeBase58(str.c_str(), vchRet, max_ret_len); | return DecodeBase58(str.c_str(), vchRet, max_ret_len); | ||||
} | } | ||||
std::string EncodeBase58Check(const std::vector<uint8_t> &vchIn) { | std::string EncodeBase58Check(const std::vector<uint8_t> &vchIn) { | ||||
// add 4-byte hash check to the end | // add 4-byte hash check to the end | ||||
std::vector<uint8_t> vch(vchIn); | std::vector<uint8_t> vch(vchIn); | ||||
uint256 hash = Hash(vch.begin(), vch.end()); | uint256 hash = Hash(vch.begin(), vch.end()); | ||||
vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); | vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); | ||||
Show All 17 Lines | if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) { | ||||
return false; | return false; | ||||
} | } | ||||
vchRet.resize(vchRet.size() - 4); | vchRet.resize(vchRet.size() - 4); | ||||
return true; | return true; | ||||
} | } | ||||
bool DecodeBase58Check(const std::string &str, std::vector<uint8_t> &vchRet, | bool DecodeBase58Check(const std::string &str, std::vector<uint8_t> &vchRet, | ||||
int max_ret) { | int max_ret) { | ||||
if (!ValidAsCString(str)) { | |||||
return false; | |||||
} | |||||
return DecodeBase58Check(str.c_str(), vchRet, max_ret); | return DecodeBase58Check(str.c_str(), vchRet, max_ret); | ||||
} | } |