Implement sendMessage and receiveMessage
This commit is contained in:
+22
-5
@@ -3,7 +3,9 @@ import pickle
|
||||
import string
|
||||
import hmac
|
||||
from dataclasses import dataclass
|
||||
from types import SimpleNamespace
|
||||
|
||||
from cryptography.exceptions import InvalidTag
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||
@@ -170,12 +172,28 @@ class MessengerClient:
|
||||
self.certs[name] = public_key
|
||||
|
||||
def sendMessage(self, name, message):
|
||||
raise Exception("not implemented!")
|
||||
return
|
||||
state = self.conns.get(name)
|
||||
if state is None:
|
||||
state = SimpleNamespace()
|
||||
self.conns[name] = state
|
||||
initial_secret = DH(self.dh, self.certs[name])
|
||||
self.RatchetInitAlice(state, initial_secret, self.certs[name])
|
||||
plaintext = message.encode("utf-8")
|
||||
return self.RatchetEncrypt(state, plaintext, b"")
|
||||
|
||||
def receiveMessage(self, name, header, ciphertext):
|
||||
raise Exception("not implemented!")
|
||||
return
|
||||
state = self.conns.get(name)
|
||||
if state is None:
|
||||
state = SimpleNamespace()
|
||||
initial_secret = DH(self.dh, self.certs[name])
|
||||
self.conns[name] = state
|
||||
self.RatchetInitBob(state, initial_secret, self.dh)
|
||||
try:
|
||||
plaintext = self.RatchetDecrypt(state, header, ciphertext, b"")
|
||||
except InvalidTag:
|
||||
return None
|
||||
return plaintext.decode("utf-8")
|
||||
|
||||
|
||||
# this is technically hybrid encryption instead of just el gamal but I blame the instructions instead of myself
|
||||
# maybe i'll change this if I want to use a late day
|
||||
@@ -204,4 +222,3 @@ class MessengerClient:
|
||||
"ciphertext": ciphertext
|
||||
}
|
||||
return plaintext.decode("latin1"), pickle.dumps(data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user