oxen-core/src/common/base32z.cpp
Doyle e3a6f20f85 Code review
- constexpr functions in common/loki.h for inlining
- move hex functions out from common/loki.h to common/hex.h
- use and apply prev_txid on LNS TX's to all LNS types (for updating in the future)
- add lns burn type, for custom burn amounts
- accept and validate lokinet addresses via base32z
- return lokinet addresses in RPC LNS calls via base32z
- updated Messenger references to Session
- update documentation to note that only Session LNS entries are allowed currently
- remove raw c-string interface from LNS db
- update multi-SQL queries into single SQL queries
- remove tx estimation backlog in anticipation for 2 priorities only, blink + unimportant
2020-02-13 11:07:47 +11:00

53 lines
1.3 KiB
C++

#include "base32z.h"
#include "crypto/crypto.h"
#include <unordered_map>
namespace base32z
{
static const std::unordered_map<char, uint8_t> zbase32_reverse_alpha = {
{'y', 0}, {'b', 1}, {'n', 2}, {'d', 3}, {'r', 4}, {'f', 5}, {'g', 6}, {'8', 7},
{'e', 8}, {'j', 9}, {'k', 10}, {'m', 11}, {'c', 12}, {'p', 13}, {'q', 14}, {'x', 15},
{'o', 16}, {'t', 17}, {'1', 18}, {'u', 19}, {'w', 20}, {'i', 21}, {'s', 22}, {'z', 23},
{'a', 24}, {'3', 25}, {'4', 26}, {'5', 27}, {'h', 28}, {'7', 29}, {'6', 30}, {'9', 31}};
static size_t decode_size(size_t sz)
{
auto d = div(sz, 5);
if (d.rem) d.quot++;
return 8 * d.quot;
}
bool decode(std::string const &src, crypto::ed25519_public_key &dest)
{
int tmp = 0, bits = 0;
size_t idx = 0;
const size_t len = decode_size(sizeof(dest));
const size_t out_len = sizeof(dest);
for (size_t i = 0; i < len; i++)
{
char ch = src[i];
if (ch)
{
auto itr = zbase32_reverse_alpha.find(ch);
if (itr == zbase32_reverse_alpha.end()) return false;
ch = itr->second;
}
else
{
return idx == out_len;
}
tmp |= ch;
bits += 5;
if (bits >= 8)
{
if (idx >= out_len) return false;
dest.data[idx] = tmp >> (bits - 8);
bits -= 8;
idx++;
}
tmp <<= 5;
}
return idx == out_len;
}
}