/**
 * 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()));
}