Electroneum
tools::base58 Namespace Reference

Functions

std::string encode (const std::string &data)
 
bool decode (const std::string &enc, std::string &data)
 
std::string encode_addr (uint64_t tag, const std::string &data)
 
bool decode_addr (const std::string &addr, uint64_t &tag, std::string &data)
 

Function Documentation

◆ decode()

bool tools::base58::decode ( const std::string &  enc,
std::string &  data 
)

Definition at line 196 of file base58.cpp.

197  {
198  if (enc.empty())
199  {
200  data.clear();
201  return true;
202  }
203 
204  size_t full_block_count = enc.size() / full_encoded_block_size;
205  size_t last_block_size = enc.size() % full_encoded_block_size;
206  int last_block_decoded_size = decoded_block_sizes::instance(last_block_size);
207  if (last_block_decoded_size < 0)
208  return false; // Invalid enc length
209  size_t data_size = full_block_count * full_block_size + last_block_decoded_size;
210 
211  data.resize(data_size, 0);
212  for (size_t i = 0; i < full_block_count; ++i)
213  {
214  if (!decode_block(enc.data() + i * full_encoded_block_size, full_encoded_block_size, &data[i * full_block_size]))
215  return false;
216  }
217 
218  if (0 < last_block_size)
219  {
220  if (!decode_block(enc.data() + full_block_count * full_encoded_block_size, last_block_size,
221  &data[full_block_count * full_block_size]))
222  return false;
223  }
224 
225  return true;
226  }
Here is the caller graph for this function:

◆ decode_addr()

bool tools::base58::decode_addr ( const std::string &  addr,
uint64_t tag,
std::string &  data 
)

Definition at line 238 of file base58.cpp.

239  {
240  std::string addr_data;
241  bool r = decode(addr, addr_data);
242  if (!r) return false;
243  if (addr_data.size() <= addr_checksum_size) return false;
244 
245  std::string checksum(addr_checksum_size, '\0');
246  checksum = addr_data.substr(addr_data.size() - addr_checksum_size);
247 
248  addr_data.resize(addr_data.size() - addr_checksum_size);
249  crypto::hash hash = crypto::cn_fast_hash(addr_data.data(), addr_data.size());
250  std::string expected_checksum(reinterpret_cast<const char*>(&hash), addr_checksum_size);
251  if (expected_checksum != checksum) return false;
252 
253  int read = tools::read_varint(addr_data.begin(), addr_data.end(), tag);
254  if (read <= 0) return false;
255 
256  data = addr_data.substr(read);
257  return true;
258  }
::std::string string
Definition: gtest-port.h:1097
bool decode(const std::string &enc, std::string &data)
Definition: base58.cpp:196
void cn_fast_hash(const void *data, size_t length, char *hash)
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode()

std::string tools::base58::encode ( const std::string &  data)

Definition at line 173 of file base58.cpp.

174  {
175  if (data.empty())
176  return std::string();
177 
178  size_t full_block_count = data.size() / full_block_size;
179  size_t last_block_size = data.size() % full_block_size;
180  size_t res_size = full_block_count * full_encoded_block_size + encoded_block_sizes[last_block_size];
181 
182  std::string res(res_size, alphabet[0]);
183  for (size_t i = 0; i < full_block_count; ++i)
184  {
185  encode_block(data.data() + i * full_block_size, full_block_size, &res[i * full_encoded_block_size]);
186  }
187 
188  if (0 < last_block_size)
189  {
190  encode_block(data.data() + full_block_count * full_block_size, last_block_size, &res[full_block_count * full_encoded_block_size]);
191  }
192 
193  return res;
194  }
const char * res
Definition: hmac_keccak.cpp:41
::std::string string
Definition: gtest-port.h:1097
Here is the caller graph for this function:

◆ encode_addr()

std::string tools::base58::encode_addr ( uint64_t  tag,
const std::string &  data 
)

Definition at line 228 of file base58.cpp.

229  {
231  buf += data;
232  crypto::hash hash = crypto::cn_fast_hash(buf.data(), buf.size());
233  const char* hash_data = reinterpret_cast<const char*>(&hash);
234  buf.append(hash_data, addr_checksum_size);
235  return encode(buf);
236  }
::std::string string
Definition: gtest-port.h:1097
std::string encode(const std::string &data)
Definition: base58.cpp:173
std::string get_varint_data(const T &v)
Returns the string that represents the varint.
Definition: varint.h:85
const char * buf
Definition: slow_memmem.cpp:74
void cn_fast_hash(const void *data, size_t length, char *hash)
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:
Here is the caller graph for this function: