| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 if (type == kAmericanExpressCard && number.size() != 15) | 71 if (type == kAmericanExpressCard && number.size() != 15) |
| 72 return false; | 72 return false; |
| 73 if (type == kDinersCard && number.size() != 14) | 73 if (type == kDinersCard && number.size() != 14) |
| 74 return false; | 74 return false; |
| 75 if (type == kDiscoverCard && number.size() != 16) | 75 if (type == kDiscoverCard && number.size() != 16) |
| 76 return false; | 76 return false; |
| 77 if (type == kJCBCard && number.size() != 16) | 77 if (type == kJCBCard && number.size() != 16) |
| 78 return false; | 78 return false; |
| 79 if (type == kMasterCard && number.size() != 16) | 79 if (type == kMasterCard && number.size() != 16) |
| 80 return false; | 80 return false; |
| 81 if (type == kUnionPay && number.size() != 16) |
| 82 return false; |
| 81 if (type == kVisaCard && number.size() != 13 && number.size() != 16) | 83 if (type == kVisaCard && number.size() != 13 && number.size() != 16) |
| 82 return false; | 84 return false; |
| 83 if (type == kGenericCard && (number.size() < 12 || number.size() > 19)) | 85 if (type == kGenericCard && (number.size() < 12 || number.size() > 19)) |
| 84 return false; | 86 return false; |
| 85 | 87 |
| 88 // Unlike all the other supported types, UnionPay cards lack Luhn checksum |
| 89 // validation. |
| 90 if (type == kUnionPay) |
| 91 return true; |
| 92 |
| 86 // Use the Luhn formula [3] to validate the number. | 93 // Use the Luhn formula [3] to validate the number. |
| 87 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm | 94 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm |
| 88 int sum = 0; | 95 int sum = 0; |
| 89 bool odd = false; | 96 bool odd = false; |
| 90 for (base::string16::reverse_iterator iter = number.rbegin(); | 97 for (base::string16::reverse_iterator iter = number.rbegin(); |
| 91 iter != number.rend(); | 98 iter != number.rend(); |
| 92 ++iter) { | 99 ++iter) { |
| 93 if (!IsAsciiDigit(*iter)) | 100 if (!IsAsciiDigit(*iter)) |
| 94 return false; | 101 return false; |
| 95 | 102 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 number_string.begin() + 9), | 211 number_string.begin() + 9), |
| 205 &serial) | 212 &serial) |
| 206 || serial == 0) { | 213 || serial == 0) { |
| 207 return false; | 214 return false; |
| 208 } | 215 } |
| 209 | 216 |
| 210 return true; | 217 return true; |
| 211 } | 218 } |
| 212 | 219 |
| 213 } // namespace autofill | 220 } // namespace autofill |
| OLD | NEW |