1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef COMPONENTS_OS_CRYPT_OS_CRYPT_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define COMPONENTS_OS_CRYPT_OS_CRYPT_H_ 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <string> 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/strings/string16.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The OSCrypt class gives access to simple encryption and decryption of 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// strings. Note that on Mac, access to the system Keychain is required and 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// these calls can block the current thread to collect user input. 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class OSCrypt { 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public: 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Encrypt a string16. The output (second argument) is really an array of 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // bytes, but we're passing it back as a std::string. 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static bool EncryptString16(const base::string16& plaintext, 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) std::string* ciphertext); 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Decrypt an array of bytes obtained with EncryptString16 back into a 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // string16. Note that the input (first argument) is a std::string, so you 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // need to first get your (binary) data into a string. 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static bool DecryptString16(const std::string& ciphertext, 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::string16* plaintext); 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Encrypt a string. 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static bool EncryptString(const std::string& plaintext, 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) std::string* ciphertext); 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Decrypt an array of bytes obtained with EnctryptString back into a string. 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Note that the input (first argument) is a std::string, so you need to first 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // get your (binary) data into a string. 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static bool DecryptString(const std::string& ciphertext, 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) std::string* plaintext); 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_MACOSX) 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // For unit testing purposes we instruct the Encryptor to use a mock Keychain 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // on the Mac. The default is to use the real Keychain. 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static void UseMockKeychain(bool use_mock); 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private: 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DISALLOW_IMPLICIT_CONSTRUCTORS(OSCrypt); 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // COMPONENTS_OS_CRYPT_OS_CRYPT_H_ 49