Changeset View
Changeset View
Standalone View
Standalone View
src/base58.cpp
Show First 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | if (!ValidAsCString(str)) { | ||||
return false; | 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); | ||||
vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); | vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); | ||||
return EncodeBase58(vch); | return EncodeBase58(vch); | ||||
} | } | ||||
bool DecodeBase58Check(const char *psz, std::vector<uint8_t> &vchRet, | bool DecodeBase58Check(const char *psz, std::vector<uint8_t> &vchRet, | ||||
int max_ret_len) { | int max_ret_len) { | ||||
if (!DecodeBase58(psz, vchRet, | if (!DecodeBase58(psz, vchRet, | ||||
max_ret_len > std::numeric_limits<int>::max() - 4 | max_ret_len > std::numeric_limits<int>::max() - 4 | ||||
? std::numeric_limits<int>::max() | ? std::numeric_limits<int>::max() | ||||
: max_ret_len + 4) || | : max_ret_len + 4) || | ||||
(vchRet.size() < 4)) { | (vchRet.size() < 4)) { | ||||
vchRet.clear(); | vchRet.clear(); | ||||
return false; | return false; | ||||
} | } | ||||
// re-calculate the checksum, ensure it matches the included 4-byte checksum | // re-calculate the checksum, ensure it matches the included 4-byte checksum | ||||
uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4); | uint256 hash = Hash(MakeSpan(vchRet).first(vchRet.size() - 4)); | ||||
if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) { | if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) { | ||||
vchRet.clear(); | vchRet.clear(); | ||||
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)) { | if (!ValidAsCString(str)) { | ||||
return false; | return false; | ||||
} | } | ||||
return DecodeBase58Check(str.c_str(), vchRet, max_ret); | return DecodeBase58Check(str.c_str(), vchRet, max_ret); | ||||
} | } |