Bitcoin/address validation: Difference between revisions
Content added Content deleted
m (Minor code correction.) |
m (Minor code improvement.) |
||
Line 335: | Line 335: | ||
const std::string ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
const std::string ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
||
std::map<char, |
std::map<char, uint32_t> base_map = |
||
{ { '0', 0 }, { '1', 1 }, { '2', 2 }, { '3', 3 }, { '4', 4 }, { '5', 5 }, { '6', 6 }, { '7', 7 }, |
|||
{ '8', 8 }, { '9', 9 }, { 'a', 10 }, { 'b', 11 }, { 'c', 12 }, { 'd', 13 }, { 'e', 14 }, { 'f', 15 }, |
|||
{ 'A', 10 }, { 'B', 11 }, { 'C', 12 }, { 'D', 13 }, { 'E', 14 }, { 'F', 15 } }; |
|||
std::vector< |
std::vector<uint32_t> hex_to_bytes(const std::string& text) { |
||
std::vector< |
std::vector<uint32_t> bytes(text.size() / 2, 0); |
||
for ( uint64_t i = 0; i < text.size(); i += 2 ) { |
for ( uint64_t i = 0; i < text.size(); i += 2 ) { |
||
const |
const uint32_t first_digit = base_map[text[i]]; |
||
const |
const uint32_t second_digit = base_map[text[i + 1]]; |
||
bytes[i / 2] = |
bytes[i / 2] = ( first_digit << 4 ) + second_digit; |
||
} |
} |
||
return bytes; |
return bytes; |
||
} |
} |
||
std::string vector_to_ascii_string(const std::vector< |
std::string vector_to_ascii_string(const std::vector<uint32_t>& bytes) { |
||
std::string result = ""; |
std::string result = ""; |
||
for ( uint32_t i = 0; i < bytes.size(); ++i ) { |
for ( uint32_t i = 0; i < bytes.size(); ++i ) { |
||
Line 357: | Line 358: | ||
} |
} |
||
std::vector< |
std::vector<uint32_t> decode_base_58(const std::string& text) { |
||
std::vector< |
std::vector<uint32_t> result(25, 0); |
||
for ( const char& ch : text ) { |
for ( const char& ch : text ) { |
||
std::string::size_type index = ALPHABET.find(ch); |
std::string::size_type index = ALPHABET.find(ch); |
||
Line 381: | Line 382: | ||
} |
} |
||
std::vector< |
std::vector<uint32_t> decoded = decode_base_58(address); |
||
std::vector first21(decoded.begin(), decoded.begin() + 21); |
std::vector first21(decoded.begin(), decoded.begin() + 21); |
||
Line 387: | Line 388: | ||
std::string text = vector_to_ascii_string(first21); |
std::string text = vector_to_ascii_string(first21); |
||
std::string hash_1 = sha256.message_digest(text); |
std::string hash_1 = sha256.message_digest(text); |
||
// Convert 'hashOne' into a suitable ASCII string for the second SHA256 hash |
// Convert 'hashOne' into a suitable ASCII string for the second SHA256 hash |
||
std::vector< |
std::vector<uint32_t> bytes_1 = hex_to_bytes(hash_1); |
||
std::string ascii_1 = vector_to_ascii_string(bytes_1); |
std::string ascii_1 = vector_to_ascii_string(bytes_1); |
||
std::string hash_2 = sha256.message_digest(ascii_1); |
std::string hash_2 = sha256.message_digest(ascii_1); |
||
std::vector< |
std::vector<uint32_t> bytes_2 = hex_to_bytes(hash_2); |
||
std::vector< |
std::vector<uint32_t> checksum(bytes_2.begin(), bytes_2.begin() + 4); |
||
std::vector< |
std::vector<uint32_t> last4(decoded.begin() + 21, decoded.begin() + 25); |
||
return checksum == last4; |
return checksum == last4; |
||
} |
} |