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.
5 #include "chrome/browser/prefs/pref_hash_calculator.h"
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"
19 // Calculates an HMAC of |message| using |key|, encoded as a hexadecimal string.
20 std::string
GetDigestString(const std::string
& key
,
21 const std::string
& message
) {
22 crypto::HMAC
hmac(crypto::HMAC::SHA256
);
23 std::vector
<uint8
> digest(hmac
.DigestLength());
24 if (!hmac
.Init(key
) || !hmac
.Sign(message
, &digest
[0], digest
.size())) {
28 return base::HexEncode(&digest
[0], digest
.size());
31 // Verifies that |digest_string| is a valid HMAC of |message| using |key|.
32 // |digest_string| must be encoded as a hexadecimal string.
33 bool VerifyDigestString(const std::string
& key
,
34 const std::string
& message
,
35 const std::string
& digest_string
) {
36 crypto::HMAC
hmac(crypto::HMAC::SHA256
);
37 std::vector
<uint8
> digest
;
38 return base::HexStringToBytes(digest_string
, &digest
) &&
41 base::StringPiece(reinterpret_cast<char*>(&digest
[0]),
45 // Renders |value| as a string. |value| may be NULL, in which case the result
46 // is an empty string.
47 std::string
ValueAsString(const base::Value
* value
) {
48 // Dictionary values may contain empty lists and sub-dictionaries. Make a
49 // deep copy with those removed to make the hash more stable.
50 const base::DictionaryValue
* dict_value
;
51 scoped_ptr
<base::DictionaryValue
> canonical_dict_value
;
52 if (value
&& value
->GetAsDictionary(&dict_value
)) {
53 canonical_dict_value
.reset(dict_value
->DeepCopyWithoutEmptyChildren());
54 value
= canonical_dict_value
.get();
57 std::string value_as_string
;
59 JSONStringValueSerializer
serializer(&value_as_string
);
60 serializer
.Serialize(*value
);
63 return value_as_string
;
66 // Common helper for all hash algorithms.
67 std::string
GetMessageFromValueAndComponents(
68 const base::Value
* value
,
69 const std::vector
<std::string
>& extra_components
) {
70 return JoinString(extra_components
, "") + ValueAsString(value
);
74 // Generates a device ID based on the input device ID. The derived device ID has
75 // no useful properties beyond those of the input device ID except that it is
76 // consistent with previous implementations.
77 std::string
GenerateDeviceIdLikePrefMetricsServiceDid(
78 const std::string
& original_device_id
) {
79 if (original_device_id
.empty())
81 return StringToLowerASCII(
82 GetDigestString(original_device_id
, "PrefMetricsService"));
85 // Verifies a hash using a deprecated hash algorithm. For validating old
86 // hashes during migration.
87 bool VerifyLegacyHash(const std::string
& seed
,
88 const base::Value
* value
,
89 const std::string
& digest_string
) {
90 return VerifyDigestString(
92 GetMessageFromValueAndComponents(value
, std::vector
<std::string
>()),
98 PrefHashCalculator::PrefHashCalculator(const std::string
& seed
,
99 const std::string
& device_id
)
101 device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id
)) {}
103 std::string
PrefHashCalculator::Calculate(const std::string
& path
,
104 const base::Value
* value
) const {
105 return GetDigestString(seed_
, GetMessage(path
, value
));
108 PrefHashCalculator::ValidationResult
PrefHashCalculator::Validate(
109 const std::string
& path
,
110 const base::Value
* value
,
111 const std::string
& digest_string
) const {
112 if (VerifyDigestString(seed_
, GetMessage(path
, value
), digest_string
))
114 if (VerifyLegacyHash(seed_
, value
, digest_string
))
119 std::string
PrefHashCalculator::GetMessage(const std::string
& path
,
120 const base::Value
* value
) const {
121 std::vector
<std::string
> components
;
122 if (!device_id_
.empty())
123 components
.push_back(device_id_
);
124 components
.push_back(path
);
125 return GetMessageFromValueAndComponents(value
, components
);