| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 return false; | 54 return false; |
| 55 if (type == kMasterCard && number.size() != 16) | 55 if (type == kMasterCard && number.size() != 16) |
| 56 return false; | 56 return false; |
| 57 if (type == kUnionPay && (number.size() < 16 || number.size() > 19)) | 57 if (type == kUnionPay && (number.size() < 16 || number.size() > 19)) |
| 58 return false; | 58 return false; |
| 59 if (type == kVisaCard && number.size() != 13 && number.size() != 16) | 59 if (type == kVisaCard && number.size() != 13 && number.size() != 16) |
| 60 return false; | 60 return false; |
| 61 if (type == kGenericCard && (number.size() < 12 || number.size() > 19)) | 61 if (type == kGenericCard && (number.size() < 12 || number.size() > 19)) |
| 62 return false; | 62 return false; |
| 63 | 63 |
| 64 // Unlike all the other supported types, UnionPay cards lack Luhn checksum | |
| 65 // validation. | |
| 66 if (type == kUnionPay) | |
| 67 return true; | |
| 68 | |
| 69 // Use the Luhn formula [3] to validate the number. | 64 // Use the Luhn formula [3] to validate the number. |
| 70 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm | 65 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm |
| 71 int sum = 0; | 66 int sum = 0; |
| 72 bool odd = false; | 67 bool odd = false; |
| 73 for (base::string16::reverse_iterator iter = number.rbegin(); | 68 for (base::string16::reverse_iterator iter = number.rbegin(); |
| 74 iter != number.rend(); | 69 iter != number.rend(); |
| 75 ++iter) { | 70 ++iter) { |
| 76 if (!base::IsAsciiDigit(*iter)) | 71 if (!base::IsAsciiDigit(*iter)) |
| 77 return false; | 72 return false; |
| 78 | 73 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 number_string.begin() + 9), | 179 number_string.begin() + 9), |
| 185 &serial) | 180 &serial) |
| 186 || serial == 0) { | 181 || serial == 0) { |
| 187 return false; | 182 return false; |
| 188 } | 183 } |
| 189 | 184 |
| 190 return true; | 185 return true; |
| 191 } | 186 } |
| 192 | 187 |
| 193 } // namespace autofill | 188 } // namespace autofill |
| OLD | NEW |