Skip to content

Commit 929c35f

Browse files
committed
Added encrypt custom snippets for kotlin
1 parent 68d4460 commit 929c35f

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

pubnub-kotlin/pubnub-kotlin-docs/src/main/kotlin/com/pubnub/docs/miscellaneous/MiscellaneousOthers.kt

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package com.pubnub.docs.miscellaneous
33
import com.pubnub.api.PubNub
44
import com.pubnub.api.UserId
55
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
69
import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper
710
import com.pubnub.api.utils.Instant
811
import com.pubnub.api.utils.TimetokenUtil
912
import kotlinx.datetime.LocalDateTime
1013
import kotlinx.datetime.TimeZone
1114
import kotlinx.datetime.toInstant
1215
import kotlinx.datetime.toLocalDateTime
16+
import java.io.InputStream
1317

1418
class MiscellaneousOthers {
15-
fun createPushPayloadMethod(pubNub: PubNub) {
19+
private fun createPushPayloadMethod(pubNub: PubNub) {
1620
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#methods
1721

1822
// snippet.createPushPayloadMethod
@@ -26,7 +30,7 @@ class MiscellaneousOthers {
2630
// snippet.end
2731
}
2832

29-
fun encryptString() {
33+
private fun encryptString() {
3034
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#encrypt-part-of-message
3135

3236
// snippet.encryptString
@@ -36,7 +40,7 @@ class MiscellaneousOthers {
3640
// snippet.end
3741
}
3842

39-
fun encryptInputStream() {
43+
private fun encryptInputStream() {
4044
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-2
4145

4246
// snippet.encryptInputStream
@@ -46,7 +50,7 @@ class MiscellaneousOthers {
4650
// snippet.end
4751
}
4852

49-
fun decryptString() {
53+
private fun decryptString() {
5054
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-3
5155

5256
// snippet.decryptString
@@ -57,7 +61,7 @@ class MiscellaneousOthers {
5761
// snippet.end
5862
}
5963

60-
fun decryptInputStream() {
64+
private fun decryptInputStream() {
6165
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-4
6266

6367
// snippet.decryptInputStream
@@ -69,46 +73,46 @@ class MiscellaneousOthers {
6973
// snippet.end
7074
}
7175

72-
fun destroy(pubNub: PubNub) {
76+
private fun destroy(pubNub: PubNub) {
7377
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-5
7478

7579
// snippet.destroy
7680
pubNub.destroy()
7781
// snippet.end
7882
}
7983

80-
fun getSubscribedChannels(pubNub: PubNub) {
84+
private fun getSubscribedChannels(pubNub: PubNub) {
8185
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-6
8286

8387
// snippet.getSubscribedChannels
8488
val subscribedChannels = pubNub.getSubscribedChannels()
8589
// snippet.end
8690
}
8791

88-
fun getSubscribedChannelGroups(pubNub: PubNub) {
92+
private fun getSubscribedChannelGroups(pubNub: PubNub) {
8993

9094
// snippet.getSubscribedChannelGroups
9195
val subscribedChannelGroups = pubNub.getSubscribedChannelGroups()
9296
// snippet.end
9397
}
9498

95-
fun disconnect(pubNub: PubNub) {
99+
private fun disconnect(pubNub: PubNub) {
96100
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-7
97101

98102
// snippet.disconnect
99103
pubNub.disconnect()
100104
// snippet.end
101105
}
102106

103-
fun reconnect(pubNub: PubNub) {
107+
private fun reconnect(pubNub: PubNub) {
104108
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-8
105109

106110
// snippet.reconnect
107111
pubNub.reconnect()
108112
// snippet.end
109113
}
110114

111-
fun timetokenToInstant() {
115+
private fun timetokenToInstant() {
112116
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-9
113117

114118
// snippet.timetokenToInstant
@@ -122,7 +126,7 @@ class MiscellaneousOthers {
122126
// snippet.end
123127
}
124128

125-
fun instantToTimetoken() {
129+
private fun instantToTimetoken() {
126130
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-10
127131

128132
// snippet.instantToTimetoken
@@ -137,7 +141,7 @@ class MiscellaneousOthers {
137141
// snippet.end
138142
}
139143

140-
fun unixToTimetoken() {
144+
private fun unixToTimetoken() {
141145
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-11
142146

143147
// snippet.unixToTimetoken
@@ -152,7 +156,7 @@ class MiscellaneousOthers {
152156
// snippet.end
153157
}
154158

155-
fun timetokenToUnix() {
159+
private fun timetokenToUnix() {
156160
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-12
157161

158162
// snippet.timetokenToUnix
@@ -167,7 +171,7 @@ class MiscellaneousOthers {
167171
// snippet.end
168172
}
169173

170-
fun createCryptoModuleBasic() {
174+
private fun createCryptoModuleBasic() {
171175
// https://www.pubnub.com/docs/general/setup/data-security#encrypting-messages
172176

173177
// snippet.createCryptoModuleBasic
@@ -179,4 +183,38 @@ class MiscellaneousOthers {
179183
val pubnub = PubNub.create(config)
180184
// snippet.end
181185
}
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+
182220
}

0 commit comments

Comments
 (0)