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 |
11 RemoteDevice::RemoteDevice() {} | 35 RemoteDevice::RemoteDevice() {} |
12 | 36 |
13 RemoteDevice::RemoteDevice(const std::string& user_id, | 37 RemoteDevice::RemoteDevice(const std::string& user_id, |
14 const std::string& name, | 38 const std::string& name, |
15 const std::string& public_key, | 39 const std::string& public_key, |
16 const std::string& bluetooth_address, | 40 const std::string& bluetooth_address, |
17 const std::string& persistent_symmetric_key, | 41 const std::string& persistent_symmetric_key, |
18 std::string sign_in_challenge) | 42 std::string sign_in_challenge) |
19 : user_id(user_id), | 43 : user_id(user_id), |
20 name(name), | 44 name(name), |
21 public_key(public_key), | 45 public_key(public_key), |
22 bluetooth_address(bluetooth_address), | 46 bluetooth_address(bluetooth_address), |
23 persistent_symmetric_key(persistent_symmetric_key), | 47 persistent_symmetric_key(persistent_symmetric_key), |
24 sign_in_challenge(sign_in_challenge) {} | 48 sign_in_challenge(sign_in_challenge), |
| 49 are_beacon_seeds_loaded(false) {} |
25 | 50 |
26 RemoteDevice::RemoteDevice(const RemoteDevice& other) = default; | 51 RemoteDevice::RemoteDevice(const RemoteDevice& other) = default; |
27 | 52 |
28 RemoteDevice::~RemoteDevice() {} | 53 RemoteDevice::~RemoteDevice() {} |
29 | 54 |
| 55 void RemoteDevice::LoadBeaconSeeds( |
| 56 const std::vector<BeaconSeed>& beacon_seeds) { |
| 57 this->are_beacon_seeds_loaded = true; |
| 58 this->beacon_seeds = beacon_seeds; |
| 59 } |
| 60 |
30 std::string RemoteDevice::GetDeviceId() const { | 61 std::string RemoteDevice::GetDeviceId() const { |
31 std::string to_return; | 62 std::string to_return; |
32 base::Base64Encode(public_key, &to_return); | 63 base::Base64Encode(public_key, &to_return); |
33 return to_return; | 64 return to_return; |
34 } | 65 } |
35 | 66 |
36 std::string RemoteDevice::GetTruncatedDeviceIdForLogs() const { | 67 std::string RemoteDevice::GetTruncatedDeviceIdForLogs() const { |
37 return RemoteDevice::TruncateDeviceIdForLogs(GetDeviceId()); | 68 return RemoteDevice::TruncateDeviceIdForLogs(GetDeviceId()); |
38 } | 69 } |
39 | 70 |
40 bool RemoteDevice::operator==(const RemoteDevice& other) const { | 71 bool RemoteDevice::operator==(const RemoteDevice& other) const { |
41 return user_id == other.user_id | 72 // Only compare |beacon_seeds| if they are loaded. |
42 && name == other.name | 73 bool are_beacon_seeds_equal = false; |
43 && public_key == other.public_key | 74 if (are_beacon_seeds_loaded) { |
44 && bluetooth_address == other.bluetooth_address | 75 are_beacon_seeds_equal = |
45 && persistent_symmetric_key == other.persistent_symmetric_key | 76 other.are_beacon_seeds_loaded && |
46 && sign_in_challenge == other.sign_in_challenge; | 77 AreBeaconSeedsEqual(beacon_seeds, other.beacon_seeds); |
| 78 } else { |
| 79 are_beacon_seeds_equal = !other.are_beacon_seeds_loaded; |
| 80 } |
| 81 |
| 82 return user_id == other.user_id && name == other.name && |
| 83 public_key == other.public_key && |
| 84 bluetooth_address == other.bluetooth_address && |
| 85 persistent_symmetric_key == other.persistent_symmetric_key && |
| 86 sign_in_challenge == other.sign_in_challenge && are_beacon_seeds_equal; |
47 } | 87 } |
48 | 88 |
49 bool RemoteDevice::operator<(const RemoteDevice& other) const { | 89 bool RemoteDevice::operator<(const RemoteDevice& other) const { |
50 // |public_key| is the only field guaranteed to be set and is also unique to | 90 // |public_key| is the only field guaranteed to be set and is also unique to |
51 // each RemoteDevice. However, since it can contain null bytes, use | 91 // each RemoteDevice. However, since it can contain null bytes, use |
52 // GetDeviceId(), which cannot contain null bytes, to compare devices. | 92 // GetDeviceId(), which cannot contain null bytes, to compare devices. |
53 return GetDeviceId().compare(other.GetDeviceId()) < 0; | 93 return GetDeviceId().compare(other.GetDeviceId()) < 0; |
54 } | 94 } |
55 | 95 |
56 // static | 96 // static |
57 std::string RemoteDevice::TruncateDeviceIdForLogs(const std::string& full_id) { | 97 std::string RemoteDevice::TruncateDeviceIdForLogs(const std::string& full_id) { |
58 if (full_id.length() <= 10) { | 98 if (full_id.length() <= 10) { |
59 return full_id; | 99 return full_id; |
60 } | 100 } |
61 | 101 |
62 return full_id.substr(0, 5) | 102 return full_id.substr(0, 5) |
63 + "..." | 103 + "..." |
64 + full_id.substr(full_id.length() - 5, full_id.length()); | 104 + full_id.substr(full_id.length() - 5, full_id.length()); |
65 } | 105 } |
66 | 106 |
67 } // namespace cryptauth | 107 } // namespace cryptauth |
OLD | NEW |