From cf8afb73aa79b1ae576074321ee348853205d339 Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Wed, 14 Jun 2023 10:58:33 -0700 Subject: [PATCH] Use alphanumeric characters for PresenceDevice endpoint id generation PiperOrigin-RevId: 540321021 --- presence/BUILD | 3 +-- presence/presence_device.cc | 13 ++++++++++--- presence/presence_device_test.cc | 7 +++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/presence/BUILD b/presence/BUILD index b2434c39..4b1104a9 100644 --- a/presence/BUILD +++ b/presence/BUILD @@ -86,11 +86,10 @@ cc_library( "scan_request_builder.h", ], deps = [ - "//internal/crypto", "//internal/interop:device", + "//internal/platform:base", "//internal/platform:connection_info", "//internal/platform:logging", - "//internal/platform:types", "//internal/platform/implementation:types", "//internal/proto:credential_cc_proto", "//internal/proto:metadata_cc_proto", diff --git a/presence/presence_device.cc b/presence/presence_device.cc index 585d92c4..b53b135b 100644 --- a/presence/presence_device.cc +++ b/presence/presence_device.cc @@ -17,20 +17,27 @@ #include #include -#include "internal/platform/implementation/crypto.h" #include "internal/interop/device.h" #include "internal/platform/ble_connection_info.h" #include "internal/platform/implementation/system_clock.h" +#include "internal/platform/prng.h" #include "presence/device_motion.h" namespace nearby { namespace presence { namespace { +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'}; + std::string GenerateRandomEndpointId() { std::string result(kEndpointIdLength, 0); - crypto::RandBytes(const_cast(result.data()), - result.size()); + nearby::Prng prng; + for (int i = 0; i < kEndpointIdLength; i++) { + result[i] = kEndpointIdChars[prng.NextUint32() % sizeof(kEndpointIdChars)]; + } return result; } } // namespace diff --git a/presence/presence_device_test.cc b/presence/presence_device_test.cc index 0ad5c8d5..a9323bfb 100644 --- a/presence/presence_device_test.cc +++ b/presence/presence_device_test.cc @@ -117,6 +117,13 @@ TEST(PresenceDeviceTest, TestEndpointIdIsCorrectLength) { EXPECT_EQ(device.GetEndpointId().length(), kEndpointIdLength); } +TEST(PresenceDeviceTest, TestEndpointIdIsRandom) { + Metadata metadata = CreateTestMetadata(); + PresenceDevice device = PresenceDevice({kDefaultMotionType}, metadata); + EXPECT_EQ(device.GetEndpointId().length(), kEndpointIdLength); + EXPECT_NE(device.GetEndpointId(), std::string(kEndpointIdLength, 0)); +} + } // namespace } // namespace presence } // namespace nearby