public static function encryptMessage($message, $key, $hashKey = '')
    {
        // Test the key for string validity.
        Helpers::isString($message, 'Encryption', 'encryptMessage');
        Helpers::isString($key, 'Encryption', 'encryptMessage');
        Helpers::isString($hashKey, 'Encryption', 'encryptMessage');
        // Create a special hashed key for encryption.
        $key = Hash::hash($key, $hashKey, Constants::SECRETBOX_KEYBYTES);
        // Generate a nonce for the communication.
        $nonce = Entropy::generateNonce();
        // Serialize and encrypt the message object
        $ciphertext = \Sodium\crypto_secretbox(serialize($message), $nonce, $key);
        $nonce = base64_encode($nonce);
        $ciphertext = base64_encode($ciphertext);
        $json = json_encode(compact('nonce', 'ciphertext'));
        if (! is_string($json)) {
            throw new Exceptions\EncryptionException('Failed to encrypt message using key');
        }
        return base64_encode($json);
    }
public static function encryptMessage($message, $key, $hashKey = '')
    {
        // Test the key for string validity.
        Helpers::isString($message, 'Encryption', 'encryptMessage');
        Helpers::isString($key, 'Encryption', 'encryptMessage');
        Helpers::isString($hashKey, 'Encryption', 'encryptMessage');
        // Create a special hashed key for encryption.
        $key = Hash::hash($key, $hashKey, Constants::SECRETBOX_KEYBYTES);
        // Generate a nonce for the communication.
        $nonce = Entropy::generateNonce();
        // Serialize and encrypt the message object
        $ciphertext = \Sodium\crypto_secretbox(serialize($message), $nonce, $key);
        $nonce = base64_encode($nonce);
        $ciphertext = base64_encode($ciphertext);
        $json = json_encode(compact('nonce', 'ciphertext'));
        if (! is_string($json)) {
            throw new Exceptions\EncryptionException('Failed to encrypt message using key');
        }
        return base64_encode($json);
    }
public static function encryptMessage ( $ message , $ key , $ hashKey = '' ) { // Test the key for string validity. Helpers :: isString ( $ message , 'Encryption' , 'encryptMessage' ) ; Helpers :: isString ( $ key , 'Encryption' , 'encryptMessage' ) ; Helpers :: isString ( $ hashKey , 'Encryption' , 'encryptMessage' ) ; // Create a special hashed key for encryption. $ key = Hash :: hash ( $ key , $ hashKey , Constants :: SECRETBOX_KEYBYTES ) ; // Generate a nonce for the communication. $ nonce = Entropy :: generateNonce ( ) ; // Serialize and encrypt the message object $ ciphertext = \ Sodium \ crypto_secretbox ( serialize ( $ message ) , $ nonce , $ key ) ; $ nonce = base64_encode ( $ nonce ) ; $ ciphertext = base64_encode ( $ ciphertext ) ; $ json = json_encode ( compact ( 'nonce' , 'ciphertext' ) ) ; if ( ! is_string ( $ json ) ) { throw new Exceptions \ EncryptionException ( 'Failed to encrypt message using key' ) ; } return base64_encode ( $ json ) ; }
Returns an encrypted message in the form of a JSON string.

@param string $message The message to be encrypted.
@param string $key The key to encrypt the message with.
@param string $hashKey The key to hash the key with.
@return string The JSON string for the encrypted message.
@throws Exceptions\EncryptionException
@throws Exceptions\InvalidTypeException
@throws Exceptions\OutOfRangeException
Returns an encrypted message in the form of a JSON string .