Skip to content

Commit

Permalink
Create p2p_node.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent b297391 commit d403095
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions projects/DAPIO/oracle-node/p2p-networking/p2p_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import socket
import json
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend

class P2PNode:
def __init__(self, host: str, port: int, private_key: str, public_key: str):
self.host = host
self.port = port
self.private_key = serialization.load_pem_private_key(private_key.encode(), password=None, backend=default_backend())
self.public_key = serialization.load_pem_public_key(public_key.encode(), backend=default_backend())
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def connect(self, node: str) -> None:
self.socket.connect((node, self.port))

def send_message(self, message: str) -> None:
encrypted_message = self.encrypt_message(message)
self.socket.sendall(encrypted_message)

def receive_message(self) -> str:
encrypted_message = self.socket.recv(1024)
return self.decrypt_message(encrypted_message)

def encrypt_message(self, message: str) -> bytes:
encrypted_message = self.public_key.encrypt(
message.encode(),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return encrypted_message

def decrypt_message(self, encrypted_message: bytes) -> str:
decrypted_message = self.private_key.decrypt(
encrypted_message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return decrypted_message.decode()

0 comments on commit d403095

Please sign in to comment.