/** * Encrypts and returns the data. * * @param NostoAccountInterface $account the account to export the data for. * @param NostoExportCollectionInterface $collection the data collection to export. * @return string the encrypted data. */ public static function export(NostoAccountInterface $account, NostoExportCollectionInterface $collection) { $data = ''; // Use the first 16 chars of the SSO token as secret for encryption. $token = $account->getApiToken('sso'); if (!empty($token)) { $tokenValue = $token->getValue(); $secret = substr($tokenValue, 0, 16); if (!empty($secret)) { $iv = NostoCryptRandom::getRandomString(16); $cipher = new NostoCipher(); $cipher->setSecret($secret); $cipher->setIV($iv); $cipherText = $cipher->encrypt($collection->getJson()); // Prepend the IV to the cipher string so that nosto can parse and use it. // There is no security concern with sending the IV as plain text. $data = $iv . $cipherText; } } return $data; }
/** * Tests that order history data can be exported. */ public function testOrderHistoryExport() { $collection = new NostoExportOrderCollection(); $collection->append(new NostoOrder()); $cipher_text = NostoExporter::export($this->account, $collection); $this->specify('verify encrypted order export', function () use($collection, $cipher_text) { $cipher = new NostoCipher(); $cipher->setSecret('01098d0fc84ded7c'); $cipher->setIV(substr($cipher_text, 0, 16)); $plain_text = $cipher->decrypt(substr($cipher_text, 16)); $this->assertEquals($collection->getJson(), $plain_text); }); }