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

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

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

Powered by Google App Engine
This is Rietveld 408576698