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

Side by Side 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 windows UI bug. Created 8 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <algorithm>
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/logging.h"
7 #include "base/rand_util.h" 12 #include "base/rand_util.h"
8 13
9 const int kMinChar = 33; // First printable character '!' 14 const int kMinChar = 33; // First printable character '!'
10 const int kMaxChar = 126; // Last printable character '~' 15 const int kMaxChar = 126; // Last printable character '~'
11 const int kPasswordLength = 12; 16 const int kMinUpper = 65; // First upper case letter 'A'
17 const int kMaxUpper = 90; // Last upper case letter 'Z'
18 const int kMinLower = 97; // First lower case letter 'a'
19 const int kMaxLower = 122; // Last lower case letter 'z'
20 const int kMinDigit = 48; // First digit '0'
21 const int kMaxDigit = 57; // Last digit '9'
22 // Copy of the other printable symbols from the ASCII table since they are
23 // disjointed.
24 const char kOtherSymbols[] =
25 {'!', '\"', '#', '$', '%', '&', '\'', '(',
26 ')', '*', '+', ',', '-', '.', '/', ':',
27 ';', '<', '=', '>', '?', '@', '[', '\\',
28 ']', '^', '_', '`', '{', '|', '}', '~'};
29 const size_t kMinPasswordLength = 4;
30 const size_t kMaxPasswordLength = 15;
31
32 namespace {
33
34 // A helper function to get the length of the generated password from
35 // |max_length| retrieved from input password field.
36 size_t GetLengthFromHint(size_t max_length, size_t default_length) {
37 if (max_length >= kMinPasswordLength && max_length <= kMaxPasswordLength)
38 return max_length;
39 else
40 return default_length;
41 }
42
43 // Classic algorithm to randomly select |num_select| elements out of
44 // |num_total| elements. One description can be found at:
45 // "http://stackoverflow.com/questions/48087/select-a-random-n-elements-from-lis tt-in-c-sharp/48089#48089"
46 void GetRandomSelection(size_t num_to_select,
47 size_t num_total,
48 std::vector<size_t>* selections) {
49 DCHECK_GE(num_total, num_to_select);
50 size_t num_left = num_total;
51 size_t num_needed = num_to_select;
52 for (size_t i = 0; i < num_total && num_needed > 0; ++i) {
53 // we have probability = |num_needed| / |num_left| to select
54 // this position.
55 size_t probability = base::RandInt(0, num_left - 1);
56 if (probability < num_needed) {
57 selections->push_back(i);
58 --num_needed;
59 }
60 --num_left;
61 }
62 DCHECK_EQ(num_to_select, selections->size());
63 }
64
65 } // namespace
12 66
13 namespace autofill { 67 namespace autofill {
14 68
15 PasswordGenerator::PasswordGenerator() {} 69 const size_t PasswordGenerator::kDefaultPasswordLength = 12;
70
71 PasswordGenerator::PasswordGenerator(size_t max_length)
72 : password_length_(GetLengthFromHint(max_length, kDefaultPasswordLength)) {}
16 PasswordGenerator::~PasswordGenerator() {} 73 PasswordGenerator::~PasswordGenerator() {}
17 74
18 std::string PasswordGenerator::Generate() { 75 std::string PasswordGenerator::Generate() const {
19 std::string ret; 76 std::string ret;
20 for (int i = 0; i < kPasswordLength; i++) { 77 // First, randomly select 4 positions to hold one upper case letter,
21 ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar))); 78 // one lower case letter, one digit, and one other symbol respectively,
79 // to make sure at least one of each category of characters will be
80 // included in the password.
81 std::vector<size_t> positions;
82 GetRandomSelection(4u, password_length_, &positions);
83
84 // To enhance the strengh of the password, we random suffle the positions so
85 // that the 4 catagories can be put at a random position in it.
86 std::random_shuffle(positions.begin(), positions.end());
87
88 // Next, generate each character of the password.
89 for (size_t i = 0; i < password_length_; ++i) {
90 if (i == positions[0]) {
91 // Generate random upper case letter.
92 ret.push_back(static_cast<char>(base::RandInt(kMinUpper, kMaxUpper)));
93 } else if (i == positions[1]) {
94 // Generate random lower case letter.
95 ret.push_back(static_cast<char>(base::RandInt(kMinLower, kMaxLower)));
96 } else if (i == positions[2]) {
97 // Generate random digit.
98 ret.push_back(static_cast<char>(base::RandInt(kMinDigit, kMaxDigit)));
99 } else if (i == positions[3]) {
100 // Generate random other symbol.
101 ret.push_back(
102 kOtherSymbols[base::RandInt(0, arraysize(kOtherSymbols) - 1)]);
103 } else {
104 // Generate random character from all categories.
105 ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar)));
106 }
22 } 107 }
23 return ret; 108 return ret;
24 } 109 }
25 110
26 } // namespace autofill 111 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/browser/autofill/password_generator.h ('k') | chrome/browser/autofill/password_generator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698