| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/cryptauth/remote_device.h" | 5 #include "components/cryptauth/remote_device.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 | 8 |
| 9 namespace cryptauth { | 9 namespace cryptauth { |
| 10 | 10 |
| 11 namespace { | |
| 12 | |
| 13 // Returns true if both vectors are BeaconSeeds are equal. | |
| 14 bool AreBeaconSeedsEqual(const std::vector<BeaconSeed> beacon_seeds1, | |
| 15 const std::vector<BeaconSeed> beacon_seeds2) { | |
| 16 if (beacon_seeds1.size() != beacon_seeds2.size()) { | |
| 17 return false; | |
| 18 } | |
| 19 | |
| 20 for (size_t i = 0; i < beacon_seeds1.size(); ++i) { | |
| 21 const BeaconSeed& seed1 = beacon_seeds1[i]; | |
| 22 const BeaconSeed& seed2 = beacon_seeds2[i]; | |
| 23 if (seed1.start_time_millis() != seed2.start_time_millis() || | |
| 24 seed1.end_time_millis() != seed2.end_time_millis() || | |
| 25 seed1.data() != seed2.data()) { | |
| 26 return false; | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 return true; | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 RemoteDevice::RemoteDevice() {} | 11 RemoteDevice::RemoteDevice() {} |
| 36 | 12 |
| 37 RemoteDevice::RemoteDevice(const std::string& user_id, | 13 RemoteDevice::RemoteDevice(const std::string& user_id, |
| 38 const std::string& name, | 14 const std::string& name, |
| 39 const std::string& public_key, | 15 const std::string& public_key, |
| 40 const std::string& bluetooth_address, | 16 const std::string& bluetooth_address, |
| 41 const std::string& persistent_symmetric_key, | 17 const std::string& persistent_symmetric_key, |
| 42 std::string sign_in_challenge) | 18 std::string sign_in_challenge) |
| 43 : user_id(user_id), | 19 : user_id(user_id), |
| 44 name(name), | 20 name(name), |
| 45 public_key(public_key), | 21 public_key(public_key), |
| 46 bluetooth_address(bluetooth_address), | 22 bluetooth_address(bluetooth_address), |
| 47 persistent_symmetric_key(persistent_symmetric_key), | 23 persistent_symmetric_key(persistent_symmetric_key), |
| 48 sign_in_challenge(sign_in_challenge) {} | 24 sign_in_challenge(sign_in_challenge) {} |
| 49 | 25 |
| 50 RemoteDevice::RemoteDevice(const RemoteDevice& other) = default; | 26 RemoteDevice::RemoteDevice(const RemoteDevice& other) = default; |
| 51 | 27 |
| 52 RemoteDevice::~RemoteDevice() {} | 28 RemoteDevice::~RemoteDevice() {} |
| 53 | 29 |
| 54 void RemoteDevice::LoadBeaconSeeds( | |
| 55 const std::vector<BeaconSeed>& beacon_seeds) { | |
| 56 this->are_beacon_seeds_loaded = true; | |
| 57 this->beacon_seeds = beacon_seeds; | |
| 58 } | |
| 59 | |
| 60 std::string RemoteDevice::GetDeviceId() const { | 30 std::string RemoteDevice::GetDeviceId() const { |
| 61 std::string to_return; | 31 std::string to_return; |
| 62 base::Base64Encode(public_key, &to_return); | 32 base::Base64Encode(public_key, &to_return); |
| 63 return to_return; | 33 return to_return; |
| 64 } | 34 } |
| 65 | 35 |
| 66 std::string RemoteDevice::GetTruncatedDeviceIdForLogs() const { | 36 std::string RemoteDevice::GetTruncatedDeviceIdForLogs() const { |
| 67 return RemoteDevice::TruncateDeviceIdForLogs(GetDeviceId()); | 37 return RemoteDevice::TruncateDeviceIdForLogs(GetDeviceId()); |
| 68 } | 38 } |
| 69 | 39 |
| 70 bool RemoteDevice::operator==(const RemoteDevice& other) const { | 40 bool RemoteDevice::operator==(const RemoteDevice& other) const { |
| 71 // Only compare |beacon_seeds| if they are loaded. | 41 return user_id == other.user_id |
| 72 bool are_beacon_seeds_equal = false; | 42 && name == other.name |
| 73 if (are_beacon_seeds_loaded) { | 43 && public_key == other.public_key |
| 74 are_beacon_seeds_equal = | 44 && bluetooth_address == other.bluetooth_address |
| 75 other.are_beacon_seeds_loaded && | 45 && persistent_symmetric_key == other.persistent_symmetric_key |
| 76 AreBeaconSeedsEqual(beacon_seeds, other.beacon_seeds); | 46 && sign_in_challenge == other.sign_in_challenge; |
| 77 } else { | |
| 78 are_beacon_seeds_equal = !other.are_beacon_seeds_loaded; | |
| 79 } | |
| 80 | |
| 81 return user_id == other.user_id && name == other.name && | |
| 82 public_key == other.public_key && | |
| 83 bluetooth_address == other.bluetooth_address && | |
| 84 persistent_symmetric_key == other.persistent_symmetric_key && | |
| 85 sign_in_challenge == other.sign_in_challenge && are_beacon_seeds_equal; | |
| 86 } | 47 } |
| 87 | 48 |
| 88 bool RemoteDevice::operator<(const RemoteDevice& other) const { | 49 bool RemoteDevice::operator<(const RemoteDevice& other) const { |
| 89 // |public_key| is the only field guaranteed to be set and is also unique to | 50 // |public_key| is the only field guaranteed to be set and is also unique to |
| 90 // each RemoteDevice. However, since it can contain null bytes, use | 51 // each RemoteDevice. However, since it can contain null bytes, use |
| 91 // GetDeviceId(), which cannot contain null bytes, to compare devices. | 52 // GetDeviceId(), which cannot contain null bytes, to compare devices. |
| 92 return GetDeviceId().compare(other.GetDeviceId()) < 0; | 53 return GetDeviceId().compare(other.GetDeviceId()) < 0; |
| 93 } | 54 } |
| 94 | 55 |
| 95 // static | 56 // static |
| 96 std::string RemoteDevice::TruncateDeviceIdForLogs(const std::string& full_id) { | 57 std::string RemoteDevice::TruncateDeviceIdForLogs(const std::string& full_id) { |
| 97 if (full_id.length() <= 10) { | 58 if (full_id.length() <= 10) { |
| 98 return full_id; | 59 return full_id; |
| 99 } | 60 } |
| 100 | 61 |
| 101 return full_id.substr(0, 5) | 62 return full_id.substr(0, 5) |
| 102 + "..." | 63 + "..." |
| 103 + full_id.substr(full_id.length() - 5, full_id.length()); | 64 + full_id.substr(full_id.length() - 5, full_id.length()); |
| 104 } | 65 } |
| 105 | 66 |
| 106 } // namespace cryptauth | 67 } // namespace cryptauth |
| OLD | NEW |