OLD | NEW |
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 "components/autofill/core/browser/validation.h" | 5 #include "components/autofill/core/browser/validation.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 if (type == kUnionPay) | 81 if (type == kUnionPay) |
82 return true; | 82 return true; |
83 | 83 |
84 // Use the Luhn formula [3] to validate the number. | 84 // Use the Luhn formula [3] to validate the number. |
85 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm | 85 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm |
86 int sum = 0; | 86 int sum = 0; |
87 bool odd = false; | 87 bool odd = false; |
88 for (base::string16::reverse_iterator iter = number.rbegin(); | 88 for (base::string16::reverse_iterator iter = number.rbegin(); |
89 iter != number.rend(); | 89 iter != number.rend(); |
90 ++iter) { | 90 ++iter) { |
91 if (!IsAsciiDigit(*iter)) | 91 if (!base::IsAsciiDigit(*iter)) |
92 return false; | 92 return false; |
93 | 93 |
94 int digit = *iter - '0'; | 94 int digit = *iter - '0'; |
95 if (odd) { | 95 if (odd) { |
96 digit *= 2; | 96 digit *= 2; |
97 sum += digit / 10 + digit % 10; | 97 sum += digit / 10 + digit % 10; |
98 } else { | 98 } else { |
99 sum += digit; | 99 sum += digit; |
100 } | 100 } |
101 odd = !odd; | 101 odd = !odd; |
102 } | 102 } |
103 | 103 |
104 return (sum % 10) == 0; | 104 return (sum % 10) == 0; |
105 } | 105 } |
106 | 106 |
107 bool IsValidCreditCardSecurityCode(const base::string16& text) { | 107 bool IsValidCreditCardSecurityCode(const base::string16& text) { |
108 if (text.size() < 3U || text.size() > 4U) | 108 if (text.size() < 3U || text.size() > 4U) |
109 return false; | 109 return false; |
110 | 110 |
111 for (base::string16::const_iterator iter = text.begin(); | 111 for (base::string16::const_iterator iter = text.begin(); |
112 iter != text.end(); | 112 iter != text.end(); |
113 ++iter) { | 113 ++iter) { |
114 if (!IsAsciiDigit(*iter)) | 114 if (!base::IsAsciiDigit(*iter)) |
115 return false; | 115 return false; |
116 } | 116 } |
117 return true; | 117 return true; |
118 } | 118 } |
119 | 119 |
120 bool IsValidCreditCardSecurityCode(const base::string16& code, | 120 bool IsValidCreditCardSecurityCode(const base::string16& code, |
121 const base::string16& number) { | 121 const base::string16& number) { |
122 const char* const type = CreditCard::GetCreditCardType(number); | 122 const char* const type = CreditCard::GetCreditCardType(number); |
123 size_t required_length = 3; | 123 size_t required_length = 3; |
124 if (type == kAmericanExpressCard) | 124 if (type == kAmericanExpressCard) |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 number_string.begin() + 9), | 201 number_string.begin() + 9), |
202 &serial) | 202 &serial) |
203 || serial == 0) { | 203 || serial == 0) { |
204 return false; | 204 return false; |
205 } | 205 } |
206 | 206 |
207 return true; | 207 return true; |
208 } | 208 } |
209 | 209 |
210 } // namespace autofill | 210 } // namespace autofill |
OLD | NEW |