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

Side by Side Diff: components/user_prefs/tracked/pref_hash_calculator.cc

Issue 1227973003: Componentize //chrome/browser/prefs/tracked. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years, 4 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/prefs/tracked/pref_hash_calculator.h" 5 #include "components/user_prefs/tracked/pref_hash_calculator.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/json/json_string_value_serializer.h" 10 #include "base/json/json_string_value_serializer.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "crypto/hmac.h" 15 #include "crypto/hmac.h"
(...skipping 12 matching lines...) Expand all
28 return base::HexEncode(&digest[0], digest.size()); 28 return base::HexEncode(&digest[0], digest.size());
29 } 29 }
30 30
31 // Verifies that |digest_string| is a valid HMAC of |message| using |key|. 31 // Verifies that |digest_string| is a valid HMAC of |message| using |key|.
32 // |digest_string| must be encoded as a hexadecimal string. 32 // |digest_string| must be encoded as a hexadecimal string.
33 bool VerifyDigestString(const std::string& key, 33 bool VerifyDigestString(const std::string& key,
34 const std::string& message, 34 const std::string& message,
35 const std::string& digest_string) { 35 const std::string& digest_string) {
36 crypto::HMAC hmac(crypto::HMAC::SHA256); 36 crypto::HMAC hmac(crypto::HMAC::SHA256);
37 std::vector<uint8> digest; 37 std::vector<uint8> digest;
38 return base::HexStringToBytes(digest_string, &digest) && 38 return base::HexStringToBytes(digest_string, &digest) && hmac.Init(key) &&
39 hmac.Init(key) && 39 hmac.Verify(message,
40 hmac.Verify(message, 40 base::StringPiece(reinterpret_cast<char*>(&digest[0]),
41 base::StringPiece(reinterpret_cast<char*>(&digest[0]), 41 digest.size()));
42 digest.size()));
43 } 42 }
44 43
45 // Renders |value| as a string. |value| may be NULL, in which case the result 44 // Renders |value| as a string. |value| may be NULL, in which case the result
46 // is an empty string. This method can be expensive and its result should be 45 // is an empty string. This method can be expensive and its result should be
47 // re-used rather than recomputed where possible. 46 // re-used rather than recomputed where possible.
48 std::string ValueAsString(const base::Value* value) { 47 std::string ValueAsString(const base::Value* value) {
49 // Dictionary values may contain empty lists and sub-dictionaries. Make a 48 // Dictionary values may contain empty lists and sub-dictionaries. Make a
50 // deep copy with those removed to make the hash more stable. 49 // deep copy with those removed to make the hash more stable.
51 const base::DictionaryValue* dict_value; 50 const base::DictionaryValue* dict_value;
52 scoped_ptr<base::DictionaryValue> canonical_dict_value; 51 scoped_ptr<base::DictionaryValue> canonical_dict_value;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 return base::StringToLowerASCII( 88 return base::StringToLowerASCII(
90 GetDigestString(original_device_id, "PrefMetricsService")); 89 GetDigestString(original_device_id, "PrefMetricsService"));
91 } 90 }
92 91
93 } // namespace 92 } // namespace
94 93
95 PrefHashCalculator::PrefHashCalculator(const std::string& seed, 94 PrefHashCalculator::PrefHashCalculator(const std::string& seed,
96 const std::string& device_id) 95 const std::string& device_id)
97 : seed_(seed), 96 : seed_(seed),
98 device_id_(device_id), 97 device_id_(device_id),
99 legacy_device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {} 98 legacy_device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {
99 }
100 100
101 PrefHashCalculator::~PrefHashCalculator() {} 101 PrefHashCalculator::~PrefHashCalculator() {
102 }
102 103
103 std::string PrefHashCalculator::Calculate(const std::string& path, 104 std::string PrefHashCalculator::Calculate(const std::string& path,
104 const base::Value* value) const { 105 const base::Value* value) const {
105 return GetDigestString(seed_, 106 return GetDigestString(seed_,
106 GetMessage(device_id_, path, ValueAsString(value))); 107 GetMessage(device_id_, path, ValueAsString(value)));
107 } 108 }
108 109
109 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( 110 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate(
110 const std::string& path, 111 const std::string& path,
111 const base::Value* value, 112 const base::Value* value,
112 const std::string& digest_string) const { 113 const std::string& digest_string) const {
113 const std::string value_as_string(ValueAsString(value)); 114 const std::string value_as_string(ValueAsString(value));
114 if (VerifyDigestString(seed_, GetMessage(device_id_, path, value_as_string), 115 if (VerifyDigestString(seed_, GetMessage(device_id_, path, value_as_string),
115 digest_string)) { 116 digest_string)) {
116 return VALID; 117 return VALID;
117 } 118 }
118 if (VerifyDigestString(seed_, 119 if (VerifyDigestString(seed_,
119 GetMessage(legacy_device_id_, path, value_as_string), 120 GetMessage(legacy_device_id_, path, value_as_string),
120 digest_string)) { 121 digest_string)) {
121 return VALID_SECURE_LEGACY; 122 return VALID_SECURE_LEGACY;
122 } 123 }
123 return INVALID; 124 return INVALID;
124 } 125 }
OLDNEW
« no previous file with comments | « components/user_prefs/tracked/pref_hash_calculator.h ('k') | components/user_prefs/tracked/pref_hash_calculator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698