-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathcrypto.py
More file actions
64 lines (52 loc) · 2.53 KB
/
Copy pathcrypto.py
File metadata and controls
64 lines (52 loc) · 2.53 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from Cryptodome.Cipher import AES
from os import getenv
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.crypto import PubNubCryptoModule
from pubnub.crypto_core import PubNubAesCbcCryptor
from time import sleep
channel = 'cipher_algorithm_experiment'
def PNFactory(cipher_mode=AES.MODE_GCM, fallback_cipher_mode=AES.MODE_CBC) -> PubNub:
config = PNConfiguration()
config.publish_key = getenv('PN_KEY_PUBLISH')
config.subscribe_key = getenv('PN_KEY_SUBSCRIBE')
config.secret_key = getenv('PN_KEY_SECRET')
config.cipher_key = getenv('PN_KEY_CIPHER', 'my_secret_cipher_key')
config.user_id = 'experiment'
config.cipher_mode = cipher_mode
config.fallback_cipher_mode = fallback_cipher_mode
return PubNub(config)
# let's build history with legacy AES.CBC
pn = PNFactory(cipher_mode=AES.MODE_CBC, fallback_cipher_mode=None)
pn.publish().channel(channel).message('message encrypted with CBC').sync()
pn.publish().channel(channel).message('message encrypted with CBC').sync()
# now with upgraded config
pn = PNFactory(cipher_mode=AES.MODE_GCM, fallback_cipher_mode=AES.MODE_CBC)
pn.publish().channel(channel).message('message encrypted with GCM').sync()
pn.publish().channel(channel).message('message encrypted with GCM').sync()
# give some time to store messages
sleep(3)
# after upgrade decoding with GCM and fallback CBC
pn = PNFactory(cipher_mode=AES.MODE_GCM, fallback_cipher_mode=AES.MODE_CBC)
messages = pn.history().channel(channel).sync()
print([message.entry for message in messages.result.messages])
# before upgrade decoding with CBC and without fallback
pn = PNFactory(cipher_mode=AES.MODE_CBC, fallback_cipher_mode=None)
try:
messages = pn.history().channel(channel).sync()
print([message.entry for message in messages.result.messages])
except UnicodeDecodeError:
print('Unable to decode - Exception has been thrown')
# partial encrypt/decrypt example
config = PNConfiguration()
config.publish_key = getenv('PN_KEY_PUBLISH')
config.subscribe_key = getenv('PN_KEY_SUBSCRIBE')
config.user_id = 'experiment'
pubnub = PubNub(config) # pubnub instance without encryption
pubnub.crypto = PubNubCryptoModule({
PubNubAesCbcCryptor.CRYPTOR_ID: PubNubAesCbcCryptor('myCipherKey')
}, PubNubAesCbcCryptor)
text_to_encrypt = 'My Secret Text'
encrypted = pubnub.crypto.encrypt(text_to_encrypt) # encrypted wih AES cryptor and `myCipherKey` cipher key
decrypted = pubnub.crypto.decrypt(encrypted)
print(f'Source: {text_to_encrypt}\nEncrypted: {encrypted}\nDecrypted: {decrypted}')