Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: rlz/lib/machine_id.cc

Issue 10597002: Check-in rlz code to src\rlz from http://code.google.com/p/rlz/. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: update license and try clobber build Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « rlz/lib/machine_id.h ('k') | rlz/lib/machine_id_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« no previous file with comments | « rlz/lib/machine_id.h ('k') | rlz/lib/machine_id_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698