public function encrypt(string $message, string $key = null): string
    {
        $key = $key ?? $this->getKey();

        $nonce = random_bytes(
            SODIUM_CRYPTO_SECRETBOX_NONCEBYTES
        );

        $cipher = base64_encode(
            $nonce .
            sodium_crypto_secretbox(
                $message,
                $nonce,
                $key
            )
        );

        sodium_memzero($message);
        sodium_memzero($key);

        return $cipher;
    }
public function encrypt(string $message, string $key = null): string
    {
        $key = $key ?? $this->getKey();

        $nonce = random_bytes(
            SODIUM_CRYPTO_SECRETBOX_NONCEBYTES
        );

        $cipher = base64_encode(
            $nonce .
            sodium_crypto_secretbox(
                $message,
                $nonce,
                $key
            )
        );

        sodium_memzero($message);
        sodium_memzero($key);

        return $cipher;
    }
public function encrypt ( string $ message , string $ key = null ) : string { $ key = $ key ?? $ this -> getKey ( ) ; $ nonce = random_bytes ( SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ) ; $ cipher = base64_encode ( $ nonce . sodium_crypto_secretbox ( $ message , $ nonce , $ key ) ) ; sodium_memzero ( $ message ) ; sodium_memzero ( $ key ) ; return $ cipher ; }
Encrypt a message.

@param string $message The message to encrypt
@param string $key     The encryption key

@throws Exception Random Bytes Failure

@return string
Encrypt a message .