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 <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 int kMinPasswordLength = 4; | |
30 const int kMaxPasswordLength = 20; | |
31 | |
32 namespace { | |
33 | |
34 // Classic algorithm to randomly select |num_select| elements out of | |
35 // |num_total| elements. One description can be found at: | |
36 // "http://stackoverflow.com/questions/48087/select-a-random-n-elements-from-lis tt-in-c-sharp/48089#48089" | |
37 void RandomSelection(int num_select, | |
Ilya Sherman
2012/06/01 19:32:10
nit: Perhaps "GetRandomSelection", "num_to_select"
zysxqn
2012/06/01 22:32:54
Done.
| |
38 int num_total, | |
39 std::vector<int>* selections) { | |
40 DCHECK_GE(num_total, num_select); | |
41 int num_left = num_total; | |
42 int num_needed = num_select; | |
43 for (int i = 0; i < num_total && num_needed > 0; ++i) { | |
44 // we have probability = |num_needed| / |num_left| to select | |
45 // this position. | |
46 int probability = base::RandInt(0, num_left - 1); | |
47 if (probability < num_needed) { | |
48 selections->push_back(i); | |
49 --num_needed; | |
50 } | |
51 --num_left; | |
52 } | |
53 DCHECK_EQ(num_select, static_cast<int>(selections->size())); | |
54 } | |
55 | |
56 } // namespace | |
12 | 57 |
13 namespace autofill { | 58 namespace autofill { |
14 | 59 |
15 PasswordGenerator::PasswordGenerator() {} | 60 PasswordGenerator::PasswordGenerator() |
61 : password_length_(kDefaultPasswordLength) {} | |
62 | |
63 PasswordGenerator::PasswordGenerator(int max_length) | |
64 : password_length_(SetLength(max_length)) {} | |
65 | |
16 PasswordGenerator::~PasswordGenerator() {} | 66 PasswordGenerator::~PasswordGenerator() {} |
17 | 67 |
18 std::string PasswordGenerator::Generate() { | 68 int PasswordGenerator::SetLength(int max_length) { |
69 return max_length >= kMinPasswordLength && max_length <= kMaxPasswordLength ? | |
70 max_length : kDefaultPasswordLength; | |
Ilya Sherman
2012/06/01 19:32:10
nit: This is probably fine as is, but I think it w
zysxqn
2012/06/01 22:32:54
Done.
| |
71 } | |
72 | |
73 std::string PasswordGenerator::Generate() const { | |
19 std::string ret; | 74 std::string ret; |
20 for (int i = 0; i < kPasswordLength; i++) { | 75 // First, randomly select 4 positions to hold one upper case letter, |
21 ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar))); | 76 // one lower case letter, one digit, and one other symbol respectively, |
77 // to make sure at least one of each category of characters will be | |
78 // included in the password. | |
79 std::vector<int> positions; | |
80 RandomSelection(4, password_length_, &positions); | |
81 | |
82 // To enhance the strengh of the password, we random suffle the positions so | |
83 // that the 4 catagories can be put at a random position in it. | |
84 random_shuffle(positions.begin(), positions.end()); | |
Ilya Sherman
2012/06/01 19:32:10
nit: Shouldn't this be std::random_shuffle?
zysxqn
2012/06/01 22:32:54
Done.
| |
85 | |
86 // Next, generate each character of the password. | |
87 for (int i = 0; i < password_length_; ++i) { | |
88 if (i == positions.at(0)) { | |
Ilya Sherman
2012/06/01 19:32:10
Please don't use std::vector::at() -- it can throw
zysxqn
2012/06/01 22:32:54
Done.
| |
89 // Generate random upper case letter. | |
90 ret.push_back(static_cast<char>(base::RandInt(kMinUpper, kMaxUpper))); | |
91 } else if (i == positions.at(1)) { | |
92 // Generate random lower case letter. | |
93 ret.push_back(static_cast<char>(base::RandInt(kMinLower, kMaxLower))); | |
94 } else if (i == positions.at(2)) { | |
95 // Generate random digit. | |
96 ret.push_back(static_cast<char>(base::RandInt(kMinDigit, kMaxDigit))); | |
97 } else if (i == positions.at(3)) { | |
98 // Generate random other symbol. | |
99 ret.push_back(kOtherSymbols[base::RandInt(0, arraysize(kOtherSymbols))]); | |
100 } else { | |
101 // Generate random character from all categories. | |
102 ret.push_back(static_cast<char>(base::RandInt(kMinChar, kMaxChar))); | |
103 } | |
22 } | 104 } |
23 return ret; | 105 return ret; |
24 } | 106 } |
25 | 107 |
26 } // namespace autofill | 108 } // namespace autofill |
OLD | NEW |