Commit 8b3948b6 authored by Ivan Vilata-i-Balaguer's avatar Ivan Vilata-i-Balaguer
Browse files

Compute swarm names using Base32 of injector pubkey instead of hex.

This encoding is more amenable to human exchange shall we accept it as input,
so better start using it now and avoid having to change the general swarm name
derivation method further down the road.
parent aa4649cd
#include "bep5_swarms.h"
#include "util/str.h"
#include "util/bytes.h"
#include "util.h"
namespace ouinet { namespace bep5 {
std::string compute_injector_swarm_name(const ouinet::util::Ed25519PublicKey& pubkey, unsigned protocol_version)
{
return util::str
( "ed25519:", util::bytes::to_hex(pubkey.serialize())
( "ed25519:", util::base32up_encode(pubkey.serialize())
, "/v", protocol_version
, "/injectors");
}
......@@ -15,7 +14,7 @@ std::string compute_injector_swarm_name(const ouinet::util::Ed25519PublicKey& pu
std::string compute_bridge_swarm_name(const ouinet::util::Ed25519PublicKey& pubkey, unsigned protocol_version)
{
return util::str
( "ed25519:", util::bytes::to_hex(pubkey.serialize())
( "ed25519:", util::base32up_encode(pubkey.serialize())
, "/v", protocol_version
, "/bridges");
}
......@@ -23,7 +22,7 @@ std::string compute_bridge_swarm_name(const ouinet::util::Ed25519PublicKey& pubk
std::string compute_uri_swarm_name(const ouinet::util::Ed25519PublicKey& pubkey, unsigned protocol_version, boost::string_view key)
{
return util::str
( "ed25519:", util::bytes::to_hex(pubkey.serialize())
( "ed25519:", util::base32up_encode(pubkey.serialize())
, "/v", protocol_version
, "/uri/", key);
}
......
......@@ -9,6 +9,7 @@
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include "util/iterators/base32_from_binary.hpp"
using namespace std;
......@@ -41,6 +42,17 @@ string ouinet::util::zlib_decompress(const boost::string_view& in, sys::error_co
return zlib_filter<boost::iostreams::zlib_decompressor>(in);
}
// Based on <https://stackoverflow.com/a/28471421> by user "ltc"
// and <https://stackoverflow.com/a/10973348> by user "PiQuer".
string ouinet::util::detail::base32up_encode(const char* data, size_t size) {
using namespace boost::archive::iterators;
using It = base32_from_binary<transform_width<const char*, 5, 8>>;
It begin = data;
It end = data + size;
string out(begin, end); // encode to base32
return out; // do not add padding
}
// Based on <https://stackoverflow.com/a/28471421> by user "ltc"
// and <https://stackoverflow.com/a/10973348> by user "PiQuer".
string ouinet::util::detail::base64_encode(const char* data, size_t size) {
......
......@@ -163,11 +163,18 @@ std::string format_ep(const asio::ip::tcp::endpoint& ep) {
///////////////////////////////////////////////////////////////////////////////
namespace detail {
std::string base32up_encode(const char*, size_t);
std::string base64_encode(const char*, size_t);
}
std::string zlib_compress(const boost::string_view&);
std::string zlib_decompress(const boost::string_view&, sys::error_code&);
template<class In>
std::string base32up_encode(const In& in) { // unpadded!
return detail::base32up_encode(reinterpret_cast<const char*>(in.data()), in.size());
}
template<class In>
std::string base64_encode(const In& in) {
return detail::base64_encode(reinterpret_cast<const char*>(in.data()), in.size());
......
#ifndef BOOST_ARCHIVE_ITERATORS_BASE32_FROM_BINARY_HPP
#define BOOST_ARCHIVE_ITERATORS_BASE32_FROM_BINARY_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// base32_from_binary.hpp
// Adapted from Robert Ramsey's "base64_from_binary.hpp".
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <boost/assert.hpp>
#include <cstddef> // size_t
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
using ::size_t;
} // namespace std
#endif
#include <boost/iterator/transform_iterator.hpp>
#include <boost/archive/iterators/dataflow_exception.hpp>
namespace boost {
namespace archive {
namespace iterators {
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// convert binary integers to base32 characters
namespace detail {
template<class CharType>
struct from_5_bit {
typedef CharType result_type;
CharType operator()(CharType t) const{
static const char * lookup_table =
"abcdefghijklmnopqrstuvwxyz"
"234567";
BOOST_ASSERT(t < 32);
return lookup_table[static_cast<size_t>(t)];
}
};
} // namespace detail
// note: what we would like to do is
// template<class Base, class CharType = typename Base::value_type>
// typedef transform_iterator<
// from_5_bit<CharType>,
// transform_width<Base, 5, sizeof(Base::value_type) * 8, CharType>
// > base32_from_binary;
// but C++ won't accept this. Rather than using a "type generator" and
// using a different syntax, make a derivation which should be equivalent.
//
// Another issue addressed here is that the transform_iterator doesn't have
// a templated constructor. This makes it incompatible with the dataflow
// ideal. This is also addressed here.
//template<class Base, class CharType = typename Base::value_type>
template<
class Base,
class CharType = typename boost::iterator_value<Base>::type
>
class base32_from_binary :
public transform_iterator<
detail::from_5_bit<CharType>,
Base
>
{
friend class boost::iterator_core_access;
typedef transform_iterator<
typename detail::from_5_bit<CharType>,
Base
> super_t;
public:
// make composible buy using templated constructor
template<class T>
base32_from_binary(T start) :
super_t(
Base(static_cast< T >(start)),
detail::from_5_bit<CharType>()
)
{}
// intel 7.1 doesn't like default copy constructor
base32_from_binary(const base32_from_binary & rhs) :
super_t(
Base(rhs.base_reference()),
detail::from_5_bit<CharType>()
)
{}
// base32_from_binary(){};
};
} // namespace iterators
} // namespace archive
} // namespace boost
#endif // BOOST_ARCHIVE_ITERATORS_BASE32_FROM_BINARY_HPP
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment