Chromium Code Reviews| Index: chrome/browser/managed_mode/managed_user_passphrase.cc |
| diff --git a/chrome/browser/managed_mode/managed_user_passphrase.cc b/chrome/browser/managed_mode/managed_user_passphrase.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ba349a981754012281a956760f17398374854a38 |
| --- /dev/null |
| +++ b/chrome/browser/managed_mode/managed_user_passphrase.cc |
| @@ -0,0 +1,62 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/managed_mode/managed_user_passphrase.h" |
| + |
| +#include "base/base64.h" |
| +#include "base/logging.h" |
| +#include "base/string_util.h" |
| +#include "crypto/encryptor.h" |
| +#include "crypto/random.h" |
| +#include "crypto/symmetric_key.h" |
| + |
| +// These constants are used as parameters when calling |DeriveKeyFromPassword|. |
| +const int kNumberOfIterations = 1; |
| +const int kDerivedKeySize = 128; |
| +const int kSaltSize = 32; |
| + |
| +ManagedUserPassphrase::ManagedUserPassphrase(const std::string& salt) |
| + : salt_(salt) { |
| + if (salt_.empty()) |
| + GenerateRandomSalt(); |
| +} |
| + |
| +ManagedUserPassphrase::~ManagedUserPassphrase() { |
| +} |
| + |
| +std::string ManagedUserPassphrase::GetSalt() { |
| + return salt_; |
| +} |
| + |
| +void ManagedUserPassphrase::GenerateRandomSalt() { |
| + std::string bytes; |
| + crypto::RandBytes(WriteInto(&bytes, kSaltSize+1), kSaltSize); |
| + bool success = base::Base64Encode(bytes, &salt_); |
| + DCHECK(success); |
| +} |
| + |
| +void ManagedUserPassphrase::GenerateHashFromPassphrase( |
| + const std::string& passphrase, |
| + std::string* encoded_passphrase_hash) const { |
| + std::string passphrase_hash; |
| + GetPassphraseHash(passphrase, &passphrase_hash); |
| + bool success = base::Base64Encode(passphrase_hash, encoded_passphrase_hash); |
| + DCHECK(success); |
| +} |
| + |
| +void ManagedUserPassphrase::GetPassphraseHash( |
| + const std::string& passphrase, |
| + std::string* passphrase_hash) const { |
| + DCHECK(passphrase_hash); |
| + // Create a hash from the user-provided passphrase and our hard-coded salt. |
|
Bernhard Bauer
2013/02/07 15:33:11
Nit: the "hardcoded" part in the comment doesn't a
Adrian Kuegel
2013/02/07 16:31:26
Done.
|
| + scoped_ptr<crypto::SymmetricKey> encryption_key( |
| + crypto::SymmetricKey::DeriveKeyFromPassword( |
| + crypto::SymmetricKey::AES, |
| + passphrase, |
| + salt_, |
| + kNumberOfIterations, |
| + kDerivedKeySize)); |
| + bool success = encryption_key->GetRawKey(passphrase_hash); |
| + DCHECK(success); |
| +} |