| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "rlz/lib/machine_id.h" | 5 #include "rlz/lib/machine_id.h" |
| 6 | 6 |
| 7 #include "base/sha1.h" | 7 #include "base/sha1.h" |
| 8 #include "rlz/lib/assert.h" | 8 #include "rlz/lib/assert.h" |
| 9 #include "rlz/lib/crc8.h" | 9 #include "rlz/lib/crc8.h" |
| 10 #include "rlz/lib/string_utils.h" | 10 #include "rlz/lib/string_utils.h" |
| 11 | 11 |
| 12 namespace rlz_lib { | 12 namespace rlz_lib { |
| 13 | 13 |
| 14 bool GetMachineId(std::string* machine_id) { | 14 bool GetMachineId(std::string* machine_id) { |
| 15 if (!machine_id) | 15 if (!machine_id) |
| 16 return false; | 16 return false; |
| 17 | 17 |
| 18 static std::string calculated_id; | 18 static std::string calculated_id; |
| 19 static bool calculated = false; | 19 static bool calculated = false; |
| 20 if (calculated) { | 20 if (calculated) { |
| 21 *machine_id = calculated_id; | 21 *machine_id = calculated_id; |
| 22 return true; | 22 return true; |
| 23 } | 23 } |
| 24 | 24 |
| 25 string16 sid_string; | 25 base::string16 sid_string; |
| 26 int volume_id; | 26 int volume_id; |
| 27 if (!GetRawMachineId(&sid_string, &volume_id)) | 27 if (!GetRawMachineId(&sid_string, &volume_id)) |
| 28 return false; | 28 return false; |
| 29 | 29 |
| 30 if (!testing::GetMachineIdImpl(sid_string, volume_id, machine_id)) | 30 if (!testing::GetMachineIdImpl(sid_string, volume_id, machine_id)) |
| 31 return false; | 31 return false; |
| 32 | 32 |
| 33 calculated = true; | 33 calculated = true; |
| 34 calculated_id = *machine_id; | 34 calculated_id = *machine_id; |
| 35 return true; | 35 return true; |
| 36 } | 36 } |
| 37 | 37 |
| 38 namespace testing { | 38 namespace testing { |
| 39 | 39 |
| 40 bool GetMachineIdImpl(const string16& sid_string, | 40 bool GetMachineIdImpl(const base::string16& sid_string, |
| 41 int volume_id, | 41 int volume_id, |
| 42 std::string* machine_id) { | 42 std::string* machine_id) { |
| 43 machine_id->clear(); | 43 machine_id->clear(); |
| 44 | 44 |
| 45 // The ID should be the SID hash + the Hard Drive SNo. + checksum byte. | 45 // The ID should be the SID hash + the Hard Drive SNo. + checksum byte. |
| 46 static const int kSizeWithoutChecksum = base::kSHA1Length + sizeof(int); | 46 static const int kSizeWithoutChecksum = base::kSHA1Length + sizeof(int); |
| 47 std::basic_string<unsigned char> id_binary(kSizeWithoutChecksum + 1, 0); | 47 std::basic_string<unsigned char> id_binary(kSizeWithoutChecksum + 1, 0); |
| 48 | 48 |
| 49 if (!sid_string.empty()) { | 49 if (!sid_string.empty()) { |
| 50 // In order to be compatible with the old version of RLZ, the hash of the | 50 // In order to be compatible with the old version of RLZ, the hash of the |
| 51 // SID must be done with all the original bytes from the unicode string. | 51 // SID must be done with all the original bytes from the unicode string. |
| 52 // However, the chromebase SHA1 hash function takes only an std::string as | 52 // However, the chromebase SHA1 hash function takes only an std::string as |
| 53 // input, so the unicode string needs to be converted to std::string | 53 // input, so the unicode string needs to be converted to std::string |
| 54 // "as is". | 54 // "as is". |
| 55 size_t byte_count = sid_string.size() * sizeof(string16::value_type); | 55 size_t byte_count = sid_string.size() * sizeof(base::string16::value_type); |
| 56 const char* buffer = reinterpret_cast<const char*>(sid_string.c_str()); | 56 const char* buffer = reinterpret_cast<const char*>(sid_string.c_str()); |
| 57 std::string sid_string_buffer(buffer, byte_count); | 57 std::string sid_string_buffer(buffer, byte_count); |
| 58 | 58 |
| 59 // Note that digest can have embedded nulls. | 59 // Note that digest can have embedded nulls. |
| 60 std::string digest(base::SHA1HashString(sid_string_buffer)); | 60 std::string digest(base::SHA1HashString(sid_string_buffer)); |
| 61 VERIFY(digest.size() == base::kSHA1Length); | 61 VERIFY(digest.size() == base::kSHA1Length); |
| 62 std::copy(digest.begin(), digest.end(), id_binary.begin()); | 62 std::copy(digest.begin(), digest.end(), id_binary.begin()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Convert from int to binary (makes big-endian). | 65 // Convert from int to binary (makes big-endian). |
| 66 for (size_t i = 0; i < sizeof(int); i++) { | 66 for (size_t i = 0; i < sizeof(int); i++) { |
| 67 int shift_bits = 8 * (sizeof(int) - i - 1); | 67 int shift_bits = 8 * (sizeof(int) - i - 1); |
| 68 id_binary[base::kSHA1Length + i] = static_cast<unsigned char>( | 68 id_binary[base::kSHA1Length + i] = static_cast<unsigned char>( |
| 69 (volume_id >> shift_bits) & 0xFF); | 69 (volume_id >> shift_bits) & 0xFF); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Append the checksum byte. | 72 // Append the checksum byte. |
| 73 if (!sid_string.empty() || (0 != volume_id)) | 73 if (!sid_string.empty() || (0 != volume_id)) |
| 74 rlz_lib::Crc8::Generate(id_binary.c_str(), | 74 rlz_lib::Crc8::Generate(id_binary.c_str(), |
| 75 kSizeWithoutChecksum, | 75 kSizeWithoutChecksum, |
| 76 &id_binary[kSizeWithoutChecksum]); | 76 &id_binary[kSizeWithoutChecksum]); |
| 77 | 77 |
| 78 return rlz_lib::BytesToString( | 78 return rlz_lib::BytesToString( |
| 79 id_binary.c_str(), kSizeWithoutChecksum + 1, machine_id); | 79 id_binary.c_str(), kSizeWithoutChecksum + 1, machine_id); |
| 80 } | 80 } |
| 81 | 81 |
| 82 } // namespace testing | 82 } // namespace testing |
| 83 | 83 |
| 84 } // namespace rlz_lib | 84 } // namespace rlz_lib |
| OLD | NEW |