| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "remoting/host/host_secret.h" | 5 #include "remoting/host/host_secret.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 7 #include <string> | 9 #include <string> |
| 8 | 10 |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 12 | 14 |
| 13 namespace remoting { | 15 namespace remoting { |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 // 5 digits means 100K possible host secrets with uniform distribution, which | 19 // 5 digits means 100K possible host secrets with uniform distribution, which |
| 18 // should be enough for short-term passwords, given that we rate-limit guesses | 20 // should be enough for short-term passwords, given that we rate-limit guesses |
| 19 // in the cloud and expire access codes after a small number of attempts. | 21 // in the cloud and expire access codes after a small number of attempts. |
| 20 const int kHostSecretLength = 5; | 22 const int kHostSecretLength = 5; |
| 21 const char kHostSecretAlphabet[] = "0123456789"; | 23 const char kHostSecretAlphabet[] = "0123456789"; |
| 22 | 24 |
| 23 // Generates cryptographically strong random number in the range [0, max). | 25 // Generates cryptographically strong random number in the range [0, max). |
| 24 int CryptoRandomInt(int max) { | 26 int CryptoRandomInt(int max) { |
| 25 uint32 random_int32; | 27 uint32_t random_int32; |
| 26 base::RandBytes(&random_int32, sizeof(random_int32)); | 28 base::RandBytes(&random_int32, sizeof(random_int32)); |
| 27 return random_int32 % max; | 29 return random_int32 % max; |
| 28 } | 30 } |
| 29 | 31 |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 std::string GenerateSupportHostSecret() { | 34 std::string GenerateSupportHostSecret() { |
| 33 std::string result; | 35 std::string result; |
| 34 int alphabet_size = strlen(kHostSecretAlphabet); | 36 int alphabet_size = strlen(kHostSecretAlphabet); |
| 35 result.resize(kHostSecretLength); | 37 result.resize(kHostSecretLength); |
| 36 for (int i = 0; i < kHostSecretLength; ++i) { | 38 for (int i = 0; i < kHostSecretLength; ++i) { |
| 37 result[i] = kHostSecretAlphabet[CryptoRandomInt(alphabet_size)]; | 39 result[i] = kHostSecretAlphabet[CryptoRandomInt(alphabet_size)]; |
| 38 } | 40 } |
| 39 return result; | 41 return result; |
| 40 } | 42 } |
| 41 | 43 |
| 42 } // namespace remoting | 44 } // namespace remoting |
| OLD | NEW |