OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/prefs/pref_hash_calculator.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" |
| 15 #include "crypto/hmac.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Renders |value| as a string. |value| may be NULL, in which case the result |
| 20 // is an empty string. |
| 21 std::string ValueAsString(const base::Value* value) { |
| 22 // Dictionary values may contain empty lists and sub-dictionaries. Make a |
| 23 // deep copy with those removed to make the hash more stable. |
| 24 const base::DictionaryValue* dict_value; |
| 25 scoped_ptr<DictionaryValue> canonical_dict_value; |
| 26 if (value && value->GetAsDictionary(&dict_value)) { |
| 27 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren()); |
| 28 value = canonical_dict_value.get(); |
| 29 } |
| 30 |
| 31 std::string value_as_string; |
| 32 if (value) { |
| 33 JSONStringValueSerializer serializer(&value_as_string); |
| 34 serializer.Serialize(*value); |
| 35 } |
| 36 |
| 37 return value_as_string; |
| 38 } |
| 39 |
| 40 // Common helper for all hash algorithms. |
| 41 std::string CalculateFromValueAndComponents( |
| 42 const std::string& seed, |
| 43 const base::Value* value, |
| 44 const std::vector<std::string>& extra_components) { |
| 45 static const size_t kSHA256DigestSize = 32; |
| 46 |
| 47 std::string message = JoinString(extra_components, "") + ValueAsString(value); |
| 48 |
| 49 crypto::HMAC hmac(crypto::HMAC::SHA256); |
| 50 unsigned char digest[kSHA256DigestSize]; |
| 51 if (!hmac.Init(seed) || !hmac.Sign(message, digest, arraysize(digest))) { |
| 52 NOTREACHED(); |
| 53 return std::string(); |
| 54 } |
| 55 |
| 56 return base::HexEncode(digest, arraysize(digest)); |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
| 61 PrefHashCalculator::PrefHashCalculator(const std::string& seed, |
| 62 const std::string& device_id) |
| 63 : seed_(seed), device_id_(device_id) {} |
| 64 |
| 65 std::string PrefHashCalculator::Calculate(const std::string& path, |
| 66 const base::Value* value) const { |
| 67 std::vector<std::string> components; |
| 68 if (!device_id_.empty()) |
| 69 components.push_back(device_id_); |
| 70 components.push_back(path); |
| 71 return CalculateFromValueAndComponents(seed_, value, components); |
| 72 } |
| 73 |
| 74 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( |
| 75 const std::string& path, |
| 76 const base::Value* value, |
| 77 const std::string& hash) const { |
| 78 if (hash == Calculate(path, value)) |
| 79 return VALID; |
| 80 if (hash == CalculateLegacyHash(path, value)) |
| 81 return VALID_LEGACY; |
| 82 return INVALID; |
| 83 } |
| 84 |
| 85 std::string PrefHashCalculator::CalculateLegacyHash( |
| 86 const std::string& path, const base::Value* value) const { |
| 87 return CalculateFromValueAndComponents(seed_, |
| 88 value, |
| 89 std::vector<std::string>()); |
| 90 } |
OLD | NEW |