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