Browse Source

split encrypt and decrypt code in separated functions by paste version

Signed-off-by: r4sas <r4sas@i2pmail.org>
dependabot/add-v2-config-file
R4SAS 5 years ago
parent
commit
7e4fb0a8c4
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 42
      pbincli/format.py

42
pbincli/format.py

@ -164,9 +164,12 @@ class Paste: @@ -164,9 +164,12 @@ class Paste:
def decrypt(self):
from json import loads as json_decode
# that is wrapper which running needed function regrading to paste version
self._decryptV2() if self._version == 2 else self._decryptV1()
if self._version == 2:
def _decryptV2(self):
from json import loads as json_decode
iv = b64decode(self._data['adata'][0][0])
salt = b64decode(self._data['adata'][0][1])
key = self.__deriveKey(salt)
@ -186,13 +189,14 @@ class Paste: @@ -186,13 +189,14 @@ class Paste:
if 'attachment' in cipher_message and 'attachment_name' in cipher_message:
self._attachment = cipher_message['attachment']
self._attachment_name = cipher_message['attachment_name']
else:
def _decryptV1(self):
from sjcl import SJCL
from json import loads as json_decode
password = self.__preparePassKey()
cipher_text = json_decode(self._data['data'])
if self._debug: print("Text:\t{}\n".format(cipher_text))
text = SJCL().decrypt(cipher_text, password)
@ -215,8 +219,18 @@ class Paste: @@ -215,8 +219,18 @@ class Paste:
def encrypt(self, formatter, burnafterreading, discussion, expiration):
# that is wrapper which running needed function regrading to paste version
self._formatter = formatter
self._burnafterreading = burnafterreading
self._discussion = discussion
self._expiration = expiration
self._encryptV2() if self._version == 2 else self._encryptV1()
def _encryptV2(self):
from pbincli.utils import json_encode
if self._version == 2:
iv = get_random_bytes(CIPHER_TAG_BYTES)
salt = get_random_bytes(CIPHER_SALT_BYTES)
key = self.__deriveKey(salt)
@ -233,9 +247,9 @@ class Paste: @@ -233,9 +247,9 @@ class Paste:
'gcm',
self._compression
],
formatter,
int(discussion),
int(burnafterreading)
self._formatter,
int(self._discussion),
int(self._burnafterreading)
]
cipher_message = {'paste':self._text}
if self._attachment:
@ -245,15 +259,16 @@ class Paste: @@ -245,15 +259,16 @@ class Paste:
cipher = self.__initializeCipher(key, iv, adata)
ciphertext, tag = cipher.encrypt_and_digest(self.__compress(json_encode(cipher_message)))
self._data = {'v':2,'adata':adata,'ct':b64encode(ciphertext + tag).decode(),'meta':{'expire':expiration}}
self._data = {'v':2,'adata':adata,'ct':b64encode(ciphertext + tag).decode(),'meta':{'expire':self._expiration}}
else:
def _encryptV1(self):
from sjcl import SJCL
from pbincli.utils import json_encode
self._data = {'expire':expiration,'formatter':formatter,'burnafterreading':int(burnafterreading),'opendiscussion':int(discussion)}
self._data = {'expire':self._expiration,'formatter':self._formatter,'burnafterreading':int(self._burnafterreading),'opendiscussion':int(self._discussion)}
password = self.__preparePassKey()
if self._debug: print("Password:\t{}".format(password))
# Encrypting text
@ -271,4 +286,3 @@ class Paste: @@ -271,4 +286,3 @@ class Paste:
self._data['attachment'] = json_encode(cipherfile)
self._data['attachmentname'] = json_encode(cipherfilename)

Loading…
Cancel
Save