OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/encryptor/os_crypt.h" | |
6 | |
7 #include <windows.h> | |
8 #include <wincrypt.h> | |
9 | |
10 #include "base/strings/utf_string_conversions.h" | |
11 | |
12 #pragma comment(lib, "crypt32.lib") | |
13 | |
14 bool OSCrypt::EncryptString16(const base::string16& plaintext, | |
15 std::string* ciphertext) { | |
16 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); | |
17 } | |
18 | |
19 bool OSCrypt::DecryptString16(const std::string& ciphertext, | |
20 base::string16* plaintext) { | |
21 std::string utf8; | |
22 if (!DecryptString(ciphertext, &utf8)) | |
23 return false; | |
24 | |
25 *plaintext = base::UTF8ToUTF16(utf8); | |
26 return true; | |
27 } | |
28 | |
29 bool OSCrypt::EncryptString(const std::string& plaintext, | |
30 std::string* ciphertext) { | |
31 DATA_BLOB input; | |
32 input.pbData = const_cast<BYTE*>( | |
33 reinterpret_cast<const BYTE*>(plaintext.data())); | |
34 input.cbData = static_cast<DWORD>(plaintext.length()); | |
35 | |
36 DATA_BLOB output; | |
37 BOOL result = CryptProtectData(&input, L"", NULL, NULL, NULL, | |
38 0, &output); | |
39 if (!result) | |
40 return false; | |
41 | |
42 // this does a copy | |
43 ciphertext->assign(reinterpret_cast<std::string::value_type*>(output.pbData), | |
44 output.cbData); | |
45 | |
46 LocalFree(output.pbData); | |
47 return true; | |
48 } | |
49 | |
50 bool OSCrypt::DecryptString(const std::string& ciphertext, | |
51 std::string* plaintext) { | |
52 DATA_BLOB input; | |
53 input.pbData = const_cast<BYTE*>( | |
54 reinterpret_cast<const BYTE*>(ciphertext.data())); | |
55 input.cbData = static_cast<DWORD>(ciphertext.length()); | |
56 | |
57 DATA_BLOB output; | |
58 BOOL result = CryptUnprotectData(&input, NULL, NULL, NULL, NULL, | |
59 0, &output); | |
60 if (!result) | |
61 return false; | |
62 | |
63 plaintext->assign(reinterpret_cast<char*>(output.pbData), output.cbData); | |
64 LocalFree(output.pbData); | |
65 return true; | |
66 } | |
OLD | NEW |