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

Side by Side Diff: components/autofill/core/common/signatures_util.cc

Issue 2318533002: [Password Generation] Use signatures for form matching (Closed)
Patch Set: Rebase Created 4 years, 3 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
(Empty)
1 // Copyright 2016 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 "components/autofill/core/common/signatures_util.h"
6
7 #include "base/sha1.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/common/autofill_util.h"
11 #include "components/autofill/core/common/form_data.h"
12 #include "components/autofill/core/common/form_field_data.h"
13 #include "third_party/re2/src/re2/re2.h"
14 #include "third_party/re2/src/re2/stringpiece.h"
15 #include "url/gurl.h"
16
17 namespace autofill {
18
19 namespace {
20
21 // Strip away >= 5 consecutive digits.
22 const char kIgnorePatternInFieldName[] = "\\d{5,}";
23
24 // Returns a copy of |input| without all occurrences of
25 // |kIgnorePatternInFieldName|
26 std::string StripDigitsIfRequired(const base::string16& input) {
27 std::string return_string = base::UTF16ToUTF8(input);
28 re2::RE2::GlobalReplace(&return_string, re2::RE2(kIgnorePatternInFieldName),
29 re2::StringPiece());
30 return return_string;
31 }
32
33 } // namespace
34
35 FormSignature CalculateFormSignature(const FormData& form_data) {
36 const GURL& target_url = form_data.action;
37 const GURL& source_url = form_data.origin;
38 std::string scheme(target_url.scheme());
39 std::string host(target_url.host());
40
41 // If target host or scheme is empty, set scheme and host of source url.
42 // This is done to match the Toolbar's behavior.
43 if (scheme.empty() || host.empty()) {
44 scheme = source_url.scheme();
45 host = source_url.host();
46 }
47
48 std::string form_signature_field_names;
49
50 for (const FormFieldData& field : form_data.fields) {
51 if (!ShouldSkipField(field)) {
52 // Add all supported form fields (including with empty names) to the
53 // signature. This is a requirement for Autofill servers.
54 form_signature_field_names.append("&");
55 form_signature_field_names.append(StripDigitsIfRequired(field.name));
56 }
57 }
58
59 std::string form_string = scheme + "://" + host + "&" +
60 base::UTF16ToUTF8(form_data.name) +
61 form_signature_field_names;
62
63 return StrToHash64Bit(form_string);
64 }
65
66 FieldSignature CalculateFieldSignatureByNameAndType(
67 const base::string16& field_name,
68 const std::string& field_type) {
69 std::string name = base::UTF16ToUTF8(field_name);
70 std::string field_string = name + "&" + field_type;
71 return StrToHash32Bit(field_string);
72 }
73
74 FieldSignature CalculateFieldSignatureForField(
75 const FormFieldData& field_data) {
76 return CalculateFieldSignatureByNameAndType(field_data.name,
77 field_data.form_control_type);
78 }
79
80 uint64_t StrToHash64Bit(const std::string& str) {
81 std::string hash_bin = base::SHA1HashString(str);
82 DCHECK_EQ(base::kSHA1Length, hash_bin.length());
83
84 uint64_t hash64 = (((static_cast<uint64_t>(hash_bin[0])) & 0xFF) << 56) |
85 (((static_cast<uint64_t>(hash_bin[1])) & 0xFF) << 48) |
86 (((static_cast<uint64_t>(hash_bin[2])) & 0xFF) << 40) |
87 (((static_cast<uint64_t>(hash_bin[3])) & 0xFF) << 32) |
88 (((static_cast<uint64_t>(hash_bin[4])) & 0xFF) << 24) |
89 (((static_cast<uint64_t>(hash_bin[5])) & 0xFF) << 16) |
90 (((static_cast<uint64_t>(hash_bin[6])) & 0xFF) << 8) |
91 ((static_cast<uint64_t>(hash_bin[7])) & 0xFF);
92
93 return hash64;
94 }
95
96 uint32_t StrToHash32Bit(const std::string& str) {
97 std::string hash_bin = base::SHA1HashString(str);
98 DCHECK_EQ(base::kSHA1Length, hash_bin.length());
99
100 uint32_t hash32 = ((hash_bin[0] & 0xFF) << 24) |
101 ((hash_bin[1] & 0xFF) << 16) | ((hash_bin[2] & 0xFF) << 8) |
102 (hash_bin[3] & 0xFF);
103
104 return hash32;
105 }
106
107 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698