Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / components / os_crypt / os_crypt_win.cc
blobbbed44136579fb8f3fc037bcae87374dc1b20391
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.
5 #include "components/os_crypt/os_crypt.h"
7 #include <windows.h>
9 #include "base/strings/utf_string_conversions.h"
10 #include "crypto/wincrypt_shim.h"
12 #pragma comment(lib, "crypt32.lib")
14 bool OSCrypt::EncryptString16(const base::string16& plaintext,
15 std::string* ciphertext) {
16 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext);
19 bool OSCrypt::DecryptString16(const std::string& ciphertext,
20 base::string16* plaintext) {
21 std::string utf8;
22 if (!DecryptString(ciphertext, &utf8))
23 return false;
25 *plaintext = base::UTF8ToUTF16(utf8);
26 return true;
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());
36 DATA_BLOB output;
37 BOOL result = CryptProtectData(&input, L"", NULL, NULL, NULL,
38 0, &output);
39 if (!result)
40 return false;
42 // this does a copy
43 ciphertext->assign(reinterpret_cast<std::string::value_type*>(output.pbData),
44 output.cbData);
46 LocalFree(output.pbData);
47 return true;
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());
57 DATA_BLOB output;
58 BOOL result = CryptUnprotectData(&input, NULL, NULL, NULL, NULL,
59 0, &output);
60 if (!result)
61 return false;
63 plaintext->assign(reinterpret_cast<char*>(output.pbData), output.cbData);
64 LocalFree(output.pbData);
65 return true;