OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 // NOTE: this file is Winodws specific. |
| 6 |
| 7 #include "chrome/browser/sync/util/data_encryption.h" |
| 8 |
| 9 #include <windows.h> |
| 10 #include <wincrypt.h> |
| 11 |
| 12 #include <cstddef> |
| 13 #include <string> |
| 14 #include <vector> |
| 15 |
| 16 using std::string; |
| 17 using std::vector; |
| 18 |
| 19 vector<uint8> EncryptData(const string& data) { |
| 20 DATA_BLOB unencrypted_data, encrypted_data; |
| 21 unencrypted_data.pbData = (BYTE*)(data.data()); |
| 22 unencrypted_data.cbData = data.size(); |
| 23 |
| 24 if (!CryptProtectData(&unencrypted_data, L"", NULL, NULL, NULL, 0, |
| 25 &encrypted_data)) |
| 26 LOG(ERROR) << "Encryption fails: " << data; |
| 27 |
| 28 vector<uint8> result(encrypted_data.pbData, |
| 29 encrypted_data.pbData + encrypted_data.cbData); |
| 30 LocalFree(encrypted_data.pbData); |
| 31 return result; |
| 32 } |
| 33 |
| 34 bool DecryptData(const vector<uint8>& in_data, string* out_data) { |
| 35 DATA_BLOB encrypted_data, decrypted_data; |
| 36 encrypted_data.pbData = |
| 37 (in_data.empty() ? NULL : const_cast<BYTE*>(&in_data[0])); |
| 38 encrypted_data.cbData = in_data.size(); |
| 39 LPWSTR descrip = L""; |
| 40 |
| 41 if (!CryptUnprotectData(&encrypted_data, &descrip, NULL, NULL, NULL, 0, |
| 42 &decrypted_data)) { |
| 43 LOG(ERROR) << "Decryption fails: "; |
| 44 return false; |
| 45 } else { |
| 46 out_data->assign(reinterpret_cast<const char*>(decrypted_data.pbData), |
| 47 decrypted_data.cbData); |
| 48 LocalFree(decrypted_data.pbData); |
| 49 return true; |
| 50 } |
| 51 } |
OLD | NEW |