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

Unified Diff: chrome/browser/autofill/password_generator.cc

Issue 10458018: This CL does the following: (1) Pass the max_length attribute to the password generator; (2) Update… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix a bug in Windows UI. Created 8 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/autofill/password_generator.cc
diff --git a/chrome/browser/autofill/password_generator.cc b/chrome/browser/autofill/password_generator.cc
index 3361fb47d1dc4df19cc243c45dcd5fe97c3f1b2d..5a8f2c5902a7a19f915bea084a9e6b6a34bb68af 100644
--- a/chrome/browser/autofill/password_generator.cc
+++ b/chrome/browser/autofill/password_generator.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/autofill/password_generator.h"
+#include "base/logging.h"
#include "base/rand_util.h"
const int kMinChar = 33; // First printable character '!'
@@ -15,10 +16,61 @@ namespace autofill {
PasswordGenerator::PasswordGenerator() {}
PasswordGenerator::~PasswordGenerator() {}
-std::string PasswordGenerator::Generate() {
+std::string PasswordGenerator::Generate(int max_length) {
std::string ret;
- for (int i = 0; i < kPasswordLength; i++) {
- ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar)));
+ 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
+ // First, randomly select 4 places to hold one upper case letter,
+ // one lower case letter, one digit, and one other symbol respectively,
+ // to make sure at least one of each category of characters will be
+ // included in the password.
+ //
+ // 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.
+ // found at: "http://stackoverflow.com/questions/48087/select-a-random-n-"
+ // "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.
+ int number_left = length;
+ int number_needed = 4;
+ int position[4];
Ilya Sherman 2012/05/30 00:04:32 nit: "positions" (plural)?
zysxqn 2012/05/31 21:54:08 Done.
+ for (int i = 0; i < length; i++) {
+ if (number_needed == 0) {
+ break;
+ }
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.
+ // we have prob = number_needed / number_left to select this position.
+ 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.
+ if (prob < number_needed) {
+ 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.
+ }
+ --number_left;
+ }
+ 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.
+
+ // Next, generate each character of the password.
+ for (int i = 0; i < length; i++) {
+ if (i == position[0]) {
+ // 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
+ 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.
+ } else if (i == position[1]) {
+ // Generate random lower case letter.
+ ret.push_back(static_cast<char>(base::RandInt(97, 122)));
+ } else if (i == position[2]) {
+ // Generate random digit.
+ ret.push_back(static_cast<char>(base::RandInt(48, 57)));
+ } else if (i == position[3]) {
+ // Generate random other symbol.
+ // 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.
+ int index = base::RandInt(0, 31);
+ if (index < 15) {
+ ret.push_back(static_cast<char>(index + 33));
+ } else if (index < 22) {
+ ret.push_back(static_cast<char>(index + 43));
+ } else if (index < 28) {
+ ret.push_back(static_cast<char>(index + 69));
+ } else {
+ ret.push_back(static_cast<char>(index + 95));
+ }
+ } else {
+ // Generate random character from all categories.
+ ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar)));
+ }
}
return ret;
}

Powered by Google App Engine
This is Rietveld 408576698