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 <stdlib.h> | |
8 #include <time.h> | |
9 | |
10 #include "base/base64.h" | |
11 #include "base/logging.h" | |
12 #include "crypto/encryptor.h" | |
13 #include "crypto/symmetric_key.h" | |
14 | |
15 namespace { | |
16 | |
17 int kNumberOfIterations = 1; | |
18 int kDerivedKeySize = 128; | |
19 | |
20 } | |
21 | |
22 ManagedUserPassphrase::ManagedUserPassphrase(const std::string &salt) { | |
23 salt_ = salt; | |
24 if (salt_.size() == 0) { | |
Bernhard Bauer
2013/01/07 14:39:22
You can use `salt_.empty()` here.
| |
25 GenerateRandomSalt(); | |
26 } | |
27 } | |
28 | |
29 ManagedUserPassphrase::~ManagedUserPassphrase() {} | |
30 | |
31 std::string ManagedUserPassphrase::GetSalt() { | |
32 return salt_; | |
33 } | |
34 | |
35 void ManagedUserPassphrase::GenerateRandomSalt() { | |
36 salt_ = ""; | |
Bernhard Bauer
2013/01/07 14:39:22
salt_.clear();
| |
37 srand(time(0)); | |
Bernhard Bauer
2013/01/07 14:39:22
Please don't. I'm sure we have a cryptographic ran
| |
38 for (int i = 0; i<32; ++i) { | |
39 salt_ += rand() % 128; | |
40 } | |
41 std::string temp = salt_; | |
42 if (!base::Base64Encode(temp, &salt_)) { | |
43 NOTREACHED(); | |
44 } | |
45 } | |
46 | |
47 void ManagedUserPassphrase::GenerateHashFromPassphrase( | |
48 const std::string& passphrase, | |
49 std::string* encoded_passphrase_hash) { | |
50 DCHECK(encoded_passphrase_hash); | |
51 std::string passphrase_hash; | |
52 GetPassphraseHash(passphrase, &passphrase_hash); | |
53 if (!base::Base64Encode(passphrase_hash, encoded_passphrase_hash)) { | |
54 NOTREACHED(); | |
55 } | |
56 } | |
57 | |
58 void ManagedUserPassphrase::GetPassphraseHash(const std::string& passphrase, | |
59 std::string* passphrase_hash) { | |
60 DCHECK(passphrase_hash); | |
61 // Create a hash from the user-provided passphrase and our hard-coded salt. | |
62 scoped_ptr<crypto::SymmetricKey> encryption_key( | |
63 crypto::SymmetricKey::DeriveKeyFromPassword( | |
64 crypto::SymmetricKey::AES, | |
65 passphrase, | |
66 salt_, | |
67 kNumberOfIterations, | |
68 kDerivedKeySize)); | |
69 DCHECK(encryption_key.get()); | |
70 if (!encryption_key->GetRawKey(passphrase_hash)) { | |
71 NOTREACHED(); | |
72 } | |
73 } | |
OLD | NEW |