Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/autofill/password_generator.h" | 5 #include "chrome/browser/autofill/password_generator.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | |
| 7 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 8 | 9 |
| 9 const int kMinChar = 33; // First printable character '!' | 10 const int kMinChar = 33; // First printable character '!' |
| 10 const int kMaxChar = 126; // Last printable character '~' | 11 const int kMaxChar = 126; // Last printable character '~' |
| 11 const int kPasswordLength = 12; | 12 const int kPasswordLength = 12; |
| 12 | 13 |
| 13 namespace autofill { | 14 namespace autofill { |
| 14 | 15 |
| 15 PasswordGenerator::PasswordGenerator() {} | 16 PasswordGenerator::PasswordGenerator() {} |
| 16 PasswordGenerator::~PasswordGenerator() {} | 17 PasswordGenerator::~PasswordGenerator() {} |
| 17 | 18 |
| 18 std::string PasswordGenerator::Generate() { | 19 std::string PasswordGenerator::Generate(int max_length) { |
| 19 std::string ret; | 20 std::string ret; |
| 20 for (int i = 0; i < kPasswordLength; i++) { | 21 int length = max_length > 3 ? max_length : kPasswordLength; |
|
Ilya Sherman
2012/05/30 00:04:32
nit: The header does not give any indication that
Garrett Casto
2012/05/30 00:46:35
If maxLength isn't set on an element, it looks lik
zysxqn
2012/05/31 21:54:08
Done.
zysxqn
2012/05/31 21:54:08
Use separate kMinPasswordLength, kMaxPasswordLengt
| |
| 21 ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar))); | 22 // First, randomly select 4 places to hold one upper case letter, |
| 23 // one lower case letter, one digit, and one other symbol respectively, | |
| 24 // to make sure at least one of each category of characters will be | |
| 25 // included in the password. | |
| 26 // | |
| 27 // There is a classific algorithm for this and one description can be | |
|
Ilya Sherman
2012/05/30 00:04:32
nit: "classific" -> "classic"
Garrett Casto
2012/05/30 00:46:35
classific?
zysxqn
2012/05/31 21:54:08
Done.
zysxqn
2012/05/31 21:54:08
Done.
| |
| 28 // found at: "http://stackoverflow.com/questions/48087/select-a-random-n-" | |
| 29 // "elements-from-listt-in-c-sharp/48089#48089" | |
|
Ilya Sherman
2012/05/30 00:04:32
nit: This comment and the block implementing it sh
Ilya Sherman
2012/05/30 00:04:32
nit: URLs should not be broken across multiple lin
zysxqn
2012/05/31 21:54:08
Done.
zysxqn
2012/05/31 21:54:08
Done.
| |
| 30 int number_left = length; | |
| 31 int number_needed = 4; | |
| 32 int position[4]; | |
|
Ilya Sherman
2012/05/30 00:04:32
nit: "positions" (plural)?
zysxqn
2012/05/31 21:54:08
Done.
| |
| 33 for (int i = 0; i < length; i++) { | |
| 34 if (number_needed == 0) { | |
| 35 break; | |
| 36 } | |
|
Ilya Sherman
2012/05/30 00:04:32
nit: No need for curly braces, since this is a one
zysxqn
2012/05/31 21:54:08
Done.
| |
| 37 // we have prob = number_needed / number_left to select this position. | |
| 38 int prob = base::RandInt(0, number_left - 1); | |
|
Ilya Sherman
2012/05/30 00:04:32
nit: "prob" -> "probability" (per the style guide,
zysxqn
2012/05/31 21:54:08
Done.
| |
| 39 if (prob < number_needed) { | |
| 40 position[4 - number_needed--] = i; | |
|
Ilya Sherman
2012/05/30 00:04:32
nit: Please avoid using the "--" operator nested w
Garrett Casto
2012/05/30 00:46:35
Seconded.
zysxqn
2012/05/31 21:54:08
Done.
zysxqn
2012/05/31 21:54:08
Done.
| |
| 41 } | |
| 42 --number_left; | |
| 43 } | |
| 44 CHECK(number_needed == 0); | |
|
Ilya Sherman
2012/05/30 00:04:32
nit: DCHECK_EQ(0, number_needed)
zysxqn
2012/05/31 21:54:08
Done.
| |
| 45 | |
| 46 // Next, generate each character of the password. | |
| 47 for (int i = 0; i < length; i++) { | |
| 48 if (i == position[0]) { | |
| 49 // Generate random upper case letter. | |
|
Ilya Sherman
2012/05/30 00:04:32
This approach guarantees that the upper-case lette
zysxqn
2012/05/31 21:54:08
This is true. For solving this ideally we can rand
| |
| 50 ret.push_back(static_cast<char>(base::RandInt(65, 90))); | |
|
Ilya Sherman
2012/05/30 00:04:32
Please use named constants rather than raw numeric
zysxqn
2012/05/31 21:54:08
Done.
| |
| 51 } else if (i == position[1]) { | |
| 52 // Generate random lower case letter. | |
| 53 ret.push_back(static_cast<char>(base::RandInt(97, 122))); | |
| 54 } else if (i == position[2]) { | |
| 55 // Generate random digit. | |
| 56 ret.push_back(static_cast<char>(base::RandInt(48, 57))); | |
| 57 } else if (i == position[3]) { | |
| 58 // Generate random other symbol. | |
| 59 // There are 32 such characters from 4 intervals in ascii. | |
|
Ilya Sherman
2012/05/30 00:04:32
It might be clearer to just write out an array inc
Garrett Casto
2012/05/30 00:46:35
I was actually going to suggest that all of these
zysxqn
2012/05/31 21:54:08
Done.
zysxqn
2012/05/31 21:54:08
Done.
| |
| 60 int index = base::RandInt(0, 31); | |
| 61 if (index < 15) { | |
| 62 ret.push_back(static_cast<char>(index + 33)); | |
| 63 } else if (index < 22) { | |
| 64 ret.push_back(static_cast<char>(index + 43)); | |
| 65 } else if (index < 28) { | |
| 66 ret.push_back(static_cast<char>(index + 69)); | |
| 67 } else { | |
| 68 ret.push_back(static_cast<char>(index + 95)); | |
| 69 } | |
| 70 } else { | |
| 71 // Generate random character from all categories. | |
| 72 ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar))); | |
| 73 } | |
| 22 } | 74 } |
| 23 return ret; | 75 return ret; |
| 24 } | 76 } |
| 25 | 77 |
| 26 } // namespace autofill | 78 } // namespace autofill |
| OLD | NEW |