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; | |
15 const int kDerivedKeySize = 128; | |
16 | |
17 ManagedUserPassphrase::ManagedUserPassphrase(const std::string& salt) | |
18 : salt_(salt) { | |
19 if (salt_.empty()) | |
20 GenerateRandomSalt(); | |
21 } | |
22 | |
23 ManagedUserPassphrase::~ManagedUserPassphrase() {} | |
24 | |
25 std::string ManagedUserPassphrase::GetSalt() { | |
26 return salt_; | |
27 } | |
28 | |
29 void ManagedUserPassphrase::GenerateRandomSalt() { | |
30 std::string bytes(32, '\0'); | |
Bernhard Bauer
2013/01/08 17:43:14
Can you pull the size out into a constant as well?
Adrian Kuegel
2013/01/09 12:10:05
Done.
| |
31 crypto::RandBytes(WriteInto(&bytes, bytes.size()), bytes.size()); | |
Bernhard Bauer
2013/01/08 17:43:14
I think this will set the length of the string to
Adrian Kuegel
2013/01/09 12:10:05
Yes, you are right. The question is: which real sa
Bernhard Bauer
2013/02/04 16:13:35
32 is fine. You probably should set kSaltSize to 3
Adrian Kuegel
2013/02/05 12:12:35
Done.
| |
32 bool success = base::Base64Encode(bytes, &salt_); | |
33 DCHECK(success); | |
34 } | |
35 | |
36 void ManagedUserPassphrase::GenerateHashFromPassphrase( | |
37 const std::string& passphrase, | |
38 std::string* encoded_passphrase_hash) { | |
39 CHECK(encoded_passphrase_hash); | |
40 std::string passphrase_hash; | |
41 GetPassphraseHash(passphrase, &passphrase_hash); | |
42 bool success = base::Base64Encode(passphrase_hash, encoded_passphrase_hash); | |
43 DCHECK(success); | |
44 } | |
45 | |
46 void ManagedUserPassphrase::GetPassphraseHash(const std::string& passphrase, | |
47 std::string* passphrase_hash) { | |
48 DCHECK(passphrase_hash); | |
49 // Create a hash from the user-provided passphrase and our hard-coded salt. | |
50 scoped_ptr<crypto::SymmetricKey> encryption_key( | |
51 crypto::SymmetricKey::DeriveKeyFromPassword( | |
52 crypto::SymmetricKey::AES, | |
53 passphrase, | |
54 salt_, | |
55 kNumberOfIterations, | |
56 kDerivedKeySize)); | |
57 bool success = encryption_key->GetRawKey(passphrase_hash); | |
58 DCHECK(success); | |
59 } | |
OLD | NEW |