summaryrefslogtreecommitdiffstats
path: root/rlz/lib/machine_id.cc
diff options
context:
space:
mode:
authorjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-21 14:10:52 +0000
committerjam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-21 14:10:52 +0000
commit0a5e34a5386fa254a3b133f0a5f7667f1f790f67 (patch)
treed1dd0e80264f900004c56ad5ce6b56efea0bec49 /rlz/lib/machine_id.cc
parentb049c382ff842f546e2bcc261ce23438bc8764bf (diff)
downloadchromium_src-0a5e34a5386fa254a3b133f0a5f7667f1f790f67.zip
chromium_src-0a5e34a5386fa254a3b133f0a5f7667f1f790f67.tar.gz
chromium_src-0a5e34a5386fa254a3b133f0a5f7667f1f790f67.tar.bz2
Check-in rlz code to src\rlz from http://code.google.com/p/rlz/.
Review URL: https://chromiumcodereview.appspot.com/10597002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@143381 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'rlz/lib/machine_id.cc')
-rw-r--r--rlz/lib/machine_id.cc80
1 files changed, 80 insertions, 0 deletions
diff --git a/rlz/lib/machine_id.cc b/rlz/lib/machine_id.cc
new file mode 100644
index 0000000..48d05b9
--- /dev/null
+++ b/rlz/lib/machine_id.cc
@@ -0,0 +1,80 @@
+#include "rlz/lib/machine_id.h"
+
+#include "base/sha1.h"
+#include "rlz/lib/assert.h"
+#include "rlz/lib/crc8.h"
+#include "rlz/lib/string_utils.h"
+
+namespace rlz_lib {
+
+bool GetMachineId(std::string* machine_id) {
+ if (!machine_id)
+ return false;
+
+ static std::string calculated_id;
+ static bool calculated = false;
+ if (calculated) {
+ *machine_id = calculated_id;
+ return true;
+ }
+
+ string16 sid_string;
+ int volume_id;
+ if (!GetRawMachineId(&sid_string, &volume_id))
+ return false;
+
+ if (!testing::GetMachineIdImpl(sid_string, volume_id, machine_id))
+ return false;
+
+ calculated = true;
+ calculated_id = *machine_id;
+ return true;
+}
+
+namespace testing {
+
+bool GetMachineIdImpl(const string16& sid_string,
+ int volume_id,
+ std::string* machine_id) {
+ machine_id->clear();
+
+ // The ID should be the SID hash + the Hard Drive SNo. + checksum byte.
+ static const int kSizeWithoutChecksum = base::kSHA1Length + sizeof(int);
+ std::basic_string<unsigned char> id_binary(kSizeWithoutChecksum + 1, 0);
+
+ if (!sid_string.empty()) {
+ // In order to be compatible with the old version of RLZ, the hash of the
+ // SID must be done with all the original bytes from the unicode string.
+ // However, the chromebase SHA1 hash function takes only an std::string as
+ // input, so the unicode string needs to be converted to std::string
+ // "as is".
+ size_t byte_count = sid_string.size() * sizeof(string16::value_type);
+ const char* buffer = reinterpret_cast<const char*>(sid_string.c_str());
+ std::string sid_string_buffer(buffer, byte_count);
+
+ // Note that digest can have embedded nulls.
+ std::string digest(base::SHA1HashString(sid_string_buffer));
+ VERIFY(digest.size() == base::kSHA1Length);
+ std::copy(digest.begin(), digest.end(), id_binary.begin());
+ }
+
+ // Convert from int to binary (makes big-endian).
+ for (size_t i = 0; i < sizeof(int); i++) {
+ int shift_bits = 8 * (sizeof(int) - i - 1);
+ id_binary[base::kSHA1Length + i] = static_cast<unsigned char>(
+ (volume_id >> shift_bits) & 0xFF);
+ }
+
+ // Append the checksum byte.
+ if (!sid_string.empty() || (0 != volume_id))
+ rlz_lib::Crc8::Generate(id_binary.c_str(),
+ kSizeWithoutChecksum,
+ &id_binary[kSizeWithoutChecksum]);
+
+ return rlz_lib::BytesToString(
+ id_binary.c_str(), kSizeWithoutChecksum + 1, machine_id);
+}
+
+} // namespace testing
+
+} // namespace rlz_lib