Utility Methods API for PubNub Go SDK

The methods on this page are utility methods that don't fit into other categories.

Encrypt

This function allows to encrypt the data.

Deprecated

The key parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

utils.EncryptString(cipherKey, string, useRandomInitializationVector)
ParameterTypeRequiredDescription
cipherKeystringYesCipher key to use for decryption. If no key is provided, Cipher key provided in PNConfiguration will be considered.
stringstringYesThe data to encrypt
useRandomInitializationVectorbooleanYesWhen true the initialization vector (IV) is random for all requests (not just for file upload). When false the IV is hard-coded for all requests except for file upload.

Basic Usage

module, err := crypto.NewAesCbcCryptoModule("enigma", false)
module.Encrypt([]byte("String to be encrypted"))

Encrypt File

This function allows to encrypt the file content/data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To encrypt the file you can use the following method(s) in Go SDK.

utils.EncryptFile(cipherKey, iv, filePart, file)
ParameterTypeRequiredDescription
cipherKeyStringYesCipher key to use for encryption. If no key is provided, Cipher key provided in PNConfiguration will be considered.

This parameter is ignored.
ivstringOptionalInitalization Vector if using a hardcoded one (not recommended). Should be passed as []byte{} when not used.
filePartio.WriterYesWriter to write the encrypted contents.
fileos.FileYesReader to read the file.

Basic Usage

out, _ := os.Create("cat_picture.jpg")
file, err := os.Open(filepathInput)
if err != nil {
panic(err)
}

module, err := crypto.NewAesCbcCryptoModule("enigma", false)
module.EncryptFile("enigma", []byte{}, out, file)

Decrypt

This function allows to decrypt the data.

Deprecated

The cipherKey parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

utils.DecryptString(cipherKey, encrypted, useRandomInitializationVector)
ParameterTypeRequiredDescription
cipherKeystringYesCipher key to use for decryption. If no key is provided, Cipher key provided in PNConfiguration will be considered.
encryptedstringYesThe data to decrypt
useRandomInitializationVectorbooleanYesWhen set to true, the initialization vector (IV) is random for all requests, not just for file upload. When set to false, the IV is hard-coded for all requests except for file upload.

Basic Usage

config := pubnub.NewConfig("someUserId")
config.CipherKey = "cipherKey"
pn := pubnub.NewPubNub(config)
r, _ := utils.DecryptString(pn.config.CipherKey, encrypted, true)

module, err := crypto.NewAesCbcCryptoModule("enigma", false)
module.DecryptString("enigma", []byte{})

Decrypt File

This function allows to decrypt the file content/data.

Deprecated

The key parameter in this method is deprecated. We recommend that you configure a seprate instance of the crypto module and use it for partial encryption.

If you pass cipherKey as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Method(s)

To decrypt the file you can use the following method(s) in Go SDK.

utils.DecryptFile(cipherKey, contentLenEnc, reader, w)
ParameterTypeRequiredDescription
cipherKeyStringYesCipher key to use for decryption. If no key is provided, Cipher key provided in PNConfiguration will be considered.

This parameter is ignored.
contentLenEncint64OptionalThe total length of the file.
readerio.ReaderYesReader to read the encrypted contents.
wio.WriteCloserYesWriter to write the decrypted contents.

Basic Usage

outDec, _ := os.Open("cat_picture.jpg")
fi, _ := outDec.Stat()
contentLenEnc := fi.Size()
defer outDec.Close()

fileDec, _ := os.Create(filepathOutputDec)
defer fileDec.Close()
r, w := io.Pipe()

module, err := crypto.NewAesCbcCryptoModule("enigma", false)
module.EncryptFile("enigma", []byte{}, out, file)

Time

This function will return a 17 digit precision Unix epoch.

Algorithm constructing the timetoken
timetoken = (Unix epoch time in seconds) * 10000000

Example of creating a timetoken for a specific time and date:

08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000

Method(s)

To fetch Time you can use the following method(s) in Go SDK:

pn.Time().
QueryParam(queryParam).
Execute()
ParameterTypeRequiredDescription
QueryParammap[string]stringOptionalQueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Basic Usage

Get PubNub Timetoken

res, status, err := pn.Time().Execute()

fmt.Println(res, status, err)

Response

MethodTypeDescription
Timetokenint64Returns a date representation of current timetoken.

Create Push Payload

This method creates the push payload for use in the appropriate endpoint calls.

Method(s)

CreatePushPayload().
SetAPNSPayload(pubnub.PNAPNSData,[]pubnub.PNAPNS2Data).
SetCommonPayload(map[string]interface{}).
SetFCMPayload(pubnub.PNFCMData).
BuildPayload()
ParameterTypeRequiredDescription
SetAPNSPayloadpubnub.PNAPNSDataOptionalSet APNS Payload. Associated APNS devices will receive only the data within the pn_apns key.
SetAPNSPayload[]pubnub.PNAPNS2DataOptionalSet APNS2 Payload. Associated APNS devices will receive only the data within the pn_push key.
SetFCMPayloadpubnub.PNFCMDataOptionalSet FCM Payload. Associated FCM devices will receive only the data within the pn_gcm key.
SetCommonPayloadmap[string]interfaceOptionalSet Common Payload. Native PubNub subscribers will receive the entire object literal, including the pn_apns, pn_gcm, and common payload.
BuildPayloadYesBuilds the payload from the values set using the parameters. Returns a map[string]interface{}

Basic Usage

Create Push Payload

aps := pubnub.PNAPSData{
Alert: "apns alert",
Badge: 1,
Sound: "ding",
Custom: map[string]interface{}{
"aps_key1": "aps_value1",
"aps_key2": "aps_value2",
},
}

apns := pubnub.PNAPNSData{
APS: aps,
Custom: map[string]interface{}{
"apns_key1": "apns_value1",
"apns_key2": "apns_value2",
show all 82 lines

Response

The CreatePushPayload() operation returns a map[string]interface{} which can be passed directly to the Publish Method's Message parameter.

Last updated on