Back to SodiumCipher class

Method encrypt

public string
encrypt
(mixed $data, \Joomla\Crypt\Key $key)
Method to encrypt a data string.
Parameters
  • string $data The data string to encrypt.
  • \Joomla\Crypt\Key $key The key object to use for encryption.
Returns
  • string The encrypted data string.
Since
  • 3.8.0
-
  • \RuntimeException
Class: SodiumCipher
Project: Joomla

Method encrypt - Source code

/**
 * Method to encrypt a data string.
 *
 * @param   string  $data  The data string to encrypt.
 * @param   Key     $key   The key object to use for encryption.
 *
 * @return  string  The encrypted data string.
 *
 * @since   3.8.0
 * @throws  \RuntimeException
 */
public function encrypt($data, Key $key)
{
    // Validate key.
    if ($key->getType() !== 'sodium') {
        throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '.  Expected sodium.');
    }
    if (!$this->nonce) {
        throw new \RuntimeException('Missing nonce to decrypt data');
    }
    return Compat::crypto_box($data, $this->nonce, Compat::crypto_box_keypair_from_secretkey_and_publickey($key->getPrivate(), $key->getPublic()));
}