OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/managed_mode/managed_user_passphrase.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/logging.h" | |
9 #include "base/string_util.h" | |
10 #include "crypto/encryptor.h" | |
11 #include "crypto/random.h" | |
12 #include "crypto/symmetric_key.h" | |
13 | |
14 const int kNumberOfIterations = 1; | |
Pam (message me for reviews)
2013/01/14 15:37:50
I know it seems obvious, but please give a brief c
Adrian Kuegel
2013/01/19 01:21:32
Done.
| |
15 const int kDerivedKeySize = 128; | |
16 const int kSaltSize = 33; | |
17 | |
18 ManagedUserPassphrase::ManagedUserPassphrase(const std::string& salt) | |
19 : salt_(salt) { | |
20 if (salt_.empty()) | |
21 GenerateRandomSalt(); | |
22 } | |
23 | |
24 ManagedUserPassphrase::~ManagedUserPassphrase() { | |
25 } | |
26 | |
27 std::string ManagedUserPassphrase::GetSalt() { | |
28 return salt_; | |
29 } | |
30 | |
31 void ManagedUserPassphrase::GenerateRandomSalt() { | |
32 std::string bytes; | |
33 crypto::RandBytes(WriteInto(&bytes, kSaltSize), kSaltSize); | |
34 bool success = base::Base64Encode(bytes, &salt_); | |
35 DCHECK(success); | |
36 } | |
37 | |
38 void ManagedUserPassphrase::GenerateHashFromPassphrase( | |
39 const std::string& passphrase, | |
40 std::string* encoded_passphrase_hash) const { | |
41 std::string passphrase_hash; | |
42 GetPassphraseHash(passphrase, &passphrase_hash); | |
43 bool success = base::Base64Encode(passphrase_hash, encoded_passphrase_hash); | |
44 DCHECK(success); | |
45 } | |
46 | |
47 void ManagedUserPassphrase::GetPassphraseHash( | |
48 const std::string& passphrase, | |
49 std::string* passphrase_hash) const { | |
50 DCHECK(passphrase_hash); | |
51 // Create a hash from the user-provided passphrase and our hard-coded salt. | |
52 scoped_ptr<crypto::SymmetricKey> encryption_key( | |
53 crypto::SymmetricKey::DeriveKeyFromPassword( | |
54 crypto::SymmetricKey::AES, | |
55 passphrase, | |
56 salt_, | |
57 kNumberOfIterations, | |
58 kDerivedKeySize)); | |
Pam (message me for reviews)
2013/01/14 15:37:50
These are reversed from the order they were in as
Adrian Kuegel
2013/01/19 01:21:32
I had the order wrong first, Bernhard found the bu
| |
59 bool success = encryption_key->GetRawKey(passphrase_hash); | |
60 DCHECK(success); | |
61 } | |
OLD | NEW |