From da07b7ba77823903f6748ff10de2e739951c0425 Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Wed, 23 Jun 2021 00:52:59 -0700 Subject: [PATCH] Use A-Z0-9 for EndpointID (avoid a-z, -, _ characters) PiperOrigin-RevId: 380975403 --- cpp/core/internal/client_proxy.cc | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/cpp/core/internal/client_proxy.cc b/cpp/core/internal/client_proxy.cc index 76ebffd4..6705c24c 100644 --- a/cpp/core/internal/client_proxy.cc +++ b/cpp/core/internal/client_proxy.cc @@ -18,10 +18,8 @@ #include #include -#include "platform/base/base64_utils.h" #include "platform/base/feature_flags.h" #include "platform/base/prng.h" -#include "platform/public/crypto.h" #include "platform/public/logging.h" #include "platform/public/mutex_lock.h" #include "proto/connections_enums.pb.h" @@ -38,6 +36,11 @@ namespace connections { constexpr absl::Duration ClientProxy::kHighPowerAdvertisementEndpointIdCacheTimeout; +constexpr char kEndpointIdChars[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', + 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; + ClientProxy::ClientProxy() : client_id_(Prng().NextInt64()) {} ClientProxy::~ClientProxy() { Reset(); } @@ -64,15 +67,10 @@ std::string ClientProxy::GenerateLocalEndpointId() { return local_high_vis_mode_cache_endpoint_id_; } } - // 1) Concatenate the Random 64-bit value with "client" string. - // 2) Compute a hash of that concatenation. - // 3) Base64-encode that hash, to make it human-readable. - // 4) Use only the first kEndpointIdLength bytes to make ID. - ByteArray id_hash = Crypto::Sha256(absl::StrCat("client", prng_.NextInt64())); - std::string id = Base64Utils::Encode(id_hash).substr(0, kEndpointIdLength); - NEARBY_LOGS(INFO) << "ClientProxy [Local Endpoint Generated]: client=" - << GetClientId() << "; endpoint_id=" << id; - + std::string id; + for (int i = 0; i < kEndpointIdLength; i++) { + id += kEndpointIdChars[prng_.NextUint32() % sizeof(kEndpointIdChars)]; + } return id; }