Encryption API for Swift Native SDK

PubNub Swift Native SDK provides built-in message encryption to secure your real-time communications. This documentation covers crypto module configuration and utility methods for encrypting and decrypting messages using both legacy 128-bit and enhanced 256-bit AES-CBC encryption.

For general SDK configuration and initialization, refer to the Configuration page.

Configuration

cryptoModule configuration

To configure the cryptoModule to encrypt all messages/files, you can use the following methods in the Swift SDK:



Your client can decrypt content encrypted using either of the modules. This way, you can interact with historical messages or messages sent from older clients while encoding new messages using the more secure 256-bit AES-CBC cipher.

Older SDK versions

Apps built using the SDK versions lower than 6.1.0 will not be able to decrypt data encrypted using the 256-bit AES-CBC cipher. Make sure to update your clients or encrypt data using the legacy algorithm.

SDK Initialization required

Before using encryption methods, ensure your PubNub client is properly configured with subscribe key and user ID. See the Configuration guide for setup instructions.

Encryption methods

Encrypt

This function allows to encrypt the data.

Availability

For more information, refer to Crypto module configuration.

Method(s)

To encrypt the data you can use the following method(s) in Swift SDK.

func encrypt(data: Data)
* required
ParameterDescription
data *
Type: Data
The message to encrypt.

Sample code

Encrypt part of message
Reference code

This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.


Returns

Success

Encrypted Data received from the Foundation object.

Failure

An Error describing the failure.

Decryption methods

Decrypt

This function allows to decrypt the data.

Availability

For more information, refer to Crypto module configuration.

Method(s)

To decrypt the data you can use the following method(s) in Swift SDK.

func decrypt(data: Data) -> Result<Data, Error>
* required
ParameterDescription
data *
Type: Data
The data to decrypt.

Sample code

Decrypt part of message

Returns

Success

Initial Data which has been encrypted earlier.

Failure

An Error describing the failure.

Last updated on