You are here

public function WardenAPI::encrypt in Warden 7

Same name and namespace in other branches
  1. 6 warden_api.inc \WardenAPI::encrypt()

Encrypt a plaintext message.

Parameters

mixed $data: The data to encrypt for transport.

Return value

string The encoded message

Throws

Exception if there is a problem

1 call to WardenAPI::encrypt()
WardenAPI::postSiteData in ./warden_api.inc
Send the site data to Warden.

File

./warden_api.inc, line 104
The API for communicating with the Warden server application.

Class

WardenAPI
@file The API for communicating with the Warden server application.

Code

public function encrypt($data) {
  $plaintext = json_encode($data);
  $public_key = $this
    ->getPublicKey();
  $result = openssl_seal($plaintext, $message, $keys, array(
    $public_key,
  ));
  if ($result === FALSE || empty($keys[0]) || empty($message) || $message === $plaintext) {
    throw new Exception('Unable to encrypt a message: ' . openssl_error_string());
  }
  $envelope = (object) array(
    'key' => base64_encode($keys[0]),
    'message' => base64_encode($message),
  );
  return base64_encode(json_encode($envelope));
}