OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #ifndef BASE_CRYPTO_ENCRYPTOR_H_ | |
6 #define BASE_CRYPTO_ENCRYPTOR_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/base_api.h" | |
12 #include "build/build_config.h" | |
13 | |
14 #if defined(USE_NSS) | |
15 #include "base/crypto/scoped_nss_types.h" | |
16 #elif defined(OS_WIN) | |
17 #include "base/crypto/scoped_capi_types.h" | |
18 #endif | |
19 | |
20 namespace base { | |
21 | |
22 class SymmetricKey; | |
23 | |
24 class BASE_API Encryptor { | |
25 public: | |
26 enum Mode { | |
27 CBC | |
28 }; | |
29 Encryptor(); | |
30 virtual ~Encryptor(); | |
31 | |
32 // Initializes the encryptor using |key| and |iv|. Returns false if either the | |
33 // key or the initialization vector cannot be used. | |
34 bool Init(SymmetricKey* key, Mode mode, const std::string& iv); | |
35 | |
36 // Encrypts |plaintext| into |ciphertext|. | |
37 bool Encrypt(const std::string& plaintext, std::string* ciphertext); | |
38 | |
39 // Decrypts |ciphertext| into |plaintext|. | |
40 bool Decrypt(const std::string& ciphertext, std::string* plaintext); | |
41 | |
42 // TODO(albertb): Support streaming encryption. | |
43 | |
44 private: | |
45 SymmetricKey* key_; | |
46 Mode mode_; | |
47 | |
48 #if defined(USE_OPENSSL) | |
49 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. | |
50 const std::string& input, | |
51 std::string* output); | |
52 std::string iv_; | |
53 #elif defined(USE_NSS) | |
54 ScopedPK11Slot slot_; | |
55 ScopedSECItem param_; | |
56 #elif defined(OS_MACOSX) | |
57 bool Crypt(int /*CCOperation*/ op, | |
58 const std::string& input, | |
59 std::string* output); | |
60 | |
61 std::string iv_; | |
62 #elif defined(OS_WIN) | |
63 ScopedHCRYPTKEY capi_key_; | |
64 DWORD block_size_; | |
65 #endif | |
66 }; | |
67 | |
68 } // namespace base | |
69 | |
70 #endif // BASE_CRYPTO_ENCRYPTOR_H_ | |
OLD | NEW |