-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathcrypto_module.py
More file actions
50 lines (44 loc) · 1.78 KB
/
Copy pathcrypto_module.py
File metadata and controls
50 lines (44 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.crypto import AesCbcCryptoModule
from Cryptodome.Cipher import AES
my_cipher_key = 'myCipherKey'
my_message = 'myMessage'
# by default no configuration changes is needed
config = PNConfiguration()
config.uuid = 'myUUID'
config.cipher_key = my_cipher_key
pubnub = PubNub(config)
# message will be encrypted the same way it was encrypted previously
cbc_message = pubnub.crypto.encrypt(my_message) # new way of using cryptographic module from pubnub
decrypted = config.crypto.decrypt(my_cipher_key, cbc_message)
assert decrypted == my_message
# also no configuration changes is needed if you previously updated the cipher_mode to GCM
config = PNConfiguration()
config.uuid = 'myUUID'
config.cipher_key = my_cipher_key
config.cipher_mode = AES.MODE_GCM
config.fallback_cipher_mode = AES.MODE_CBC
pubnub = PubNub(config)
# message will be encrypted the same way it was encrypted previously
gcm_message = pubnub.crypto.encrypt(my_message) # new way of using cryptographic module from pubnub
decrypted = config.crypto.decrypt(my_cipher_key, gcm_message)
assert decrypted == my_message
# opt in to use crypto module with headers and improved entropy
config = PNConfiguration()
config.uuid = 'myUUID'
config.cipher_key = my_cipher_key
config.cipher_mode = AES.MODE_GCM
config.fallback_cipher_mode = AES.MODE_CBC
module = AesCbcCryptoModule(config)
config.crypto_module = module
pubnub = PubNub(config)
message = pubnub.crypto.encrypt(my_message)
# this encryption method is not compatible with previous crypto methods
try:
decoded = config.crypto.decrypt(my_cipher_key, message)
except Exception:
pass
# but can be decrypted with new crypto module
decrypted = pubnub.crypto.decrypt(message)
assert decrypted == my_message