@@ -3,16 +3,20 @@ package com.pubnub.docs.miscellaneous
3
3
import com.pubnub.api.PubNub
4
4
import com.pubnub.api.UserId
5
5
import com.pubnub.api.crypto.CryptoModule
6
+ import com.pubnub.api.crypto.cryptor.Cryptor
7
+ import com.pubnub.api.crypto.data.EncryptedData
8
+ import com.pubnub.api.crypto.data.EncryptedStreamData
6
9
import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper
7
10
import com.pubnub.api.utils.Instant
8
11
import com.pubnub.api.utils.TimetokenUtil
9
12
import kotlinx.datetime.LocalDateTime
10
13
import kotlinx.datetime.TimeZone
11
14
import kotlinx.datetime.toInstant
12
15
import kotlinx.datetime.toLocalDateTime
16
+ import java.io.InputStream
13
17
14
18
class MiscellaneousOthers {
15
- fun createPushPayloadMethod (pubNub : PubNub ) {
19
+ private fun createPushPayloadMethod (pubNub : PubNub ) {
16
20
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#methods
17
21
18
22
// snippet.createPushPayloadMethod
@@ -26,7 +30,7 @@ class MiscellaneousOthers {
26
30
// snippet.end
27
31
}
28
32
29
- fun encryptString () {
33
+ private fun encryptString () {
30
34
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#encrypt-part-of-message
31
35
32
36
// snippet.encryptString
@@ -36,7 +40,7 @@ class MiscellaneousOthers {
36
40
// snippet.end
37
41
}
38
42
39
- fun encryptInputStream () {
43
+ private fun encryptInputStream () {
40
44
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-2
41
45
42
46
// snippet.encryptInputStream
@@ -46,7 +50,7 @@ class MiscellaneousOthers {
46
50
// snippet.end
47
51
}
48
52
49
- fun decryptString () {
53
+ private fun decryptString () {
50
54
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-3
51
55
52
56
// snippet.decryptString
@@ -57,7 +61,7 @@ class MiscellaneousOthers {
57
61
// snippet.end
58
62
}
59
63
60
- fun decryptInputStream () {
64
+ private fun decryptInputStream () {
61
65
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-4
62
66
63
67
// snippet.decryptInputStream
@@ -69,46 +73,46 @@ class MiscellaneousOthers {
69
73
// snippet.end
70
74
}
71
75
72
- fun destroy (pubNub : PubNub ) {
76
+ private fun destroy (pubNub : PubNub ) {
73
77
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-5
74
78
75
79
// snippet.destroy
76
80
pubNub.destroy()
77
81
// snippet.end
78
82
}
79
83
80
- fun getSubscribedChannels (pubNub : PubNub ) {
84
+ private fun getSubscribedChannels (pubNub : PubNub ) {
81
85
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-6
82
86
83
87
// snippet.getSubscribedChannels
84
88
val subscribedChannels = pubNub.getSubscribedChannels()
85
89
// snippet.end
86
90
}
87
91
88
- fun getSubscribedChannelGroups (pubNub : PubNub ) {
92
+ private fun getSubscribedChannelGroups (pubNub : PubNub ) {
89
93
90
94
// snippet.getSubscribedChannelGroups
91
95
val subscribedChannelGroups = pubNub.getSubscribedChannelGroups()
92
96
// snippet.end
93
97
}
94
98
95
- fun disconnect (pubNub : PubNub ) {
99
+ private fun disconnect (pubNub : PubNub ) {
96
100
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-7
97
101
98
102
// snippet.disconnect
99
103
pubNub.disconnect()
100
104
// snippet.end
101
105
}
102
106
103
- fun reconnect (pubNub : PubNub ) {
107
+ private fun reconnect (pubNub : PubNub ) {
104
108
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-8
105
109
106
110
// snippet.reconnect
107
111
pubNub.reconnect()
108
112
// snippet.end
109
113
}
110
114
111
- fun timetokenToInstant () {
115
+ private fun timetokenToInstant () {
112
116
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-9
113
117
114
118
// snippet.timetokenToInstant
@@ -122,7 +126,7 @@ class MiscellaneousOthers {
122
126
// snippet.end
123
127
}
124
128
125
- fun instantToTimetoken () {
129
+ private fun instantToTimetoken () {
126
130
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-10
127
131
128
132
// snippet.instantToTimetoken
@@ -137,7 +141,7 @@ class MiscellaneousOthers {
137
141
// snippet.end
138
142
}
139
143
140
- fun unixToTimetoken () {
144
+ private fun unixToTimetoken () {
141
145
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-11
142
146
143
147
// snippet.unixToTimetoken
@@ -152,7 +156,7 @@ class MiscellaneousOthers {
152
156
// snippet.end
153
157
}
154
158
155
- fun timetokenToUnix () {
159
+ private fun timetokenToUnix () {
156
160
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-12
157
161
158
162
// snippet.timetokenToUnix
@@ -167,7 +171,7 @@ class MiscellaneousOthers {
167
171
// snippet.end
168
172
}
169
173
170
- fun createCryptoModuleBasic () {
174
+ private fun createCryptoModuleBasic () {
171
175
// https://www.pubnub.com/docs/general/setup/data-security#encrypting-messages
172
176
173
177
// snippet.createCryptoModuleBasic
@@ -179,4 +183,38 @@ class MiscellaneousOthers {
179
183
val pubnub = PubNub .create(config)
180
184
// snippet.end
181
185
}
186
+
187
+ private fun customCryptor () {
188
+ // https://www.pubnub.com/docs/general/setup/data-security#example-custom-cryptor-implementation
189
+
190
+ // snippet.customCryptor
191
+ fun myCustomCryptor () = object : Cryptor {
192
+ override fun id (): ByteArray {
193
+ // Should return a ByteArray of exactly 4 bytes.
194
+ return byteArrayOf(' C' .code.toByte(), ' U' .code.toByte(), ' S' .code.toByte(), ' T' .code.toByte())
195
+ }
196
+
197
+ override fun encrypt (data : ByteArray ): EncryptedData {
198
+ // implement your crypto logic
199
+ return EncryptedData (metadata = null , data = data)
200
+ }
201
+
202
+ override fun decrypt (encryptedData : EncryptedData ): ByteArray {
203
+ // implement your crypto logic
204
+ return encryptedData.data
205
+ }
206
+
207
+ override fun encryptStream (stream : InputStream ): EncryptedStreamData {
208
+ // implement your crypto logic
209
+ return EncryptedStreamData (metadata = null , stream = stream)
210
+ }
211
+
212
+ override fun decryptStream (encryptedData : EncryptedStreamData ): InputStream {
213
+ // implement your crypto logic
214
+ return encryptedData.stream
215
+ }
216
+ }
217
+ // snippet.end
218
+ }
219
+
182
220
}
0 commit comments