Skip to content

Commit b2844f0

Browse files
committed
fix: encryption module now can work not only with strings
1 parent 8042c87 commit b2844f0

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/encryption/encryption.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
------
4141
"""
4242

43-
from typing import Optional, List
43+
from typing import Optional, List, Union
4444
from pgpy import PGPKey, PGPMessage
4545
from pgpy.constants import SymmetricKeyAlgorithm
4646
from pgpy.errors import PGPError
@@ -74,7 +74,9 @@ def __init__(self, private_key_armored: str, passphrase: Optional[str] = None):
7474
else:
7575
raise ValueError("Private key locked. Passphrase needed")
7676

77-
def sign_and_encrypt(self, message: str, public_keys: List[str]) -> str:
77+
def sign_and_encrypt(
78+
self, message: Union[str, bytes], public_keys: List[str]
79+
) -> str:
7880
"""
7981
Signs and encrypts a message using the private key and recipient's public keys.
8082
@@ -139,7 +141,9 @@ def sign_and_encrypt(self, message: str, public_keys: List[str]) -> str:
139141
"your message", [public_key2, public_key3]
140142
)
141143
"""
144+
142145
pgp_message = PGPMessage.new(message)
146+
143147
if not self.private_key.is_unlocked:
144148
try:
145149
with self.private_key.unlock(self.passphrase):
@@ -159,7 +163,7 @@ def sign_and_encrypt(self, message: str, public_keys: List[str]) -> str:
159163
del sessionkey
160164
return pgp_message.__str__()
161165

162-
def decrypt(self, message: str, public_key: Optional[str] = None) -> str:
166+
def decrypt(self, message: str, public_key: Optional[str] = None) -> bytes:
163167
"""
164168
Decrypts a message using the private key.
165169
@@ -209,7 +213,10 @@ def decrypt(self, message: str, public_key: Optional[str] = None) -> str:
209213
public_key, _ = PGPKey.from_blob(public_key)
210214
public_key.verify(decrypted_message)
211215

212-
return decrypted_message.message.__str__()
216+
if isinstance(decrypted_message.message, str):
217+
return bytes(decrypted_message.message, encoding="utf-8")
218+
else:
219+
return bytes(decrypted_message.message)
213220
except PGPError as e:
214221
if (
215222
decrypted_message
@@ -221,7 +228,7 @@ def decrypt(self, message: str, public_key: Optional[str] = None) -> str:
221228
)
222229
raise ValueError("Failed to decrypt message: {}".format(str(e)))
223230

224-
def sign(self, message: str) -> str:
231+
def sign(self, message: Union[str, bytes]) -> str:
225232
"""
226233
Signs a message using the private key.
227234

packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/encryption/test_encryption.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ def test_encrypt_with_locked_private_key(self):
5858
def test_decrypt(self):
5959
encryption = Encryption(private_key2)
6060
decrypted_message = encryption.decrypt(encrypted_message)
61-
self.assertIsInstance(decrypted_message, str)
62-
self.assertEqual(decrypted_message, message)
61+
self.assertIsInstance(decrypted_message, bytes)
62+
self.assertEqual(decrypted_message.decode("utf-8"), message)
6363

6464
def test_decrypt_checking_signature(self):
6565
encryption = Encryption(private_key2)
6666
decrypted_message = encryption.decrypt(encrypted_message, public_key)
67-
self.assertIsInstance(decrypted_message, str)
68-
self.assertEqual(decrypted_message, message)
67+
self.assertIsInstance(decrypted_message, bytes)
68+
self.assertEqual(decrypted_message.decode("utf-8"), message)
6969

7070
def test_decrypt_with_locked_private_key(self):
7171
encryption = Encryption(private_key3, passphrase)
7272
decrypted_message = encryption.decrypt(encrypted_message)
73-
self.assertIsInstance(decrypted_message, str)
74-
self.assertEqual(decrypted_message, message)
73+
self.assertIsInstance(decrypted_message, bytes)
74+
self.assertEqual(decrypted_message.decode("utf-8"), message)
7575

7676
def test_decrypt_wrong_public_key(self):
7777
encryption = Encryption(private_key2)
@@ -85,8 +85,8 @@ def test_decrypt_wrong_public_key(self):
8585
def test_decrypt_unsigned_message(self):
8686
encryption = Encryption(private_key3, passphrase)
8787
decrypted_message = encryption.decrypt(encrypted_unsigned_message)
88-
self.assertIsInstance(decrypted_message, str)
89-
self.assertEqual(decrypted_message, message)
88+
self.assertIsInstance(decrypted_message, bytes)
89+
self.assertEqual(decrypted_message.decode("utf-8"), message)
9090

9191
def test_sign(self):
9292
encryption = Encryption(private_key)

0 commit comments

Comments
 (0)