diff --git a/README.md b/README.md index 9be2b23..b1612ed 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,16 @@ NoiseBuilder class provides highest level of abstraction for the package. You ca through this class' interfaces. An example for setting up NoiseBuilder could look like this: ```python +import socket + from noise.builder import NoiseBuilder +sock = socket.socket() +sock.connect(('localhost', 2000)) + # Create instance of NoiseBuilder, set up to use NN handshake pattern, Curve25519 for # elliptic curve keypair, ChaCha20Poly1305 as cipher function and SHA256 for hashing. -proto = NoiseBuilder.from_name('Noise_NN_25519_ChaChaPoly_SHA256') +proto = NoiseBuilder.from_name(b'Noise_NN_25519_ChaChaPoly_SHA256') # Set role in this connection as initiator proto.set_as_initiator() @@ -47,20 +52,62 @@ proto.start_handshake() message = proto.write_message() # Send the message to the responder - you may simply use sockets or any other way # to exchange bytes between communicating parties. -# For clarity - we omit socket creation in this example. -sock.send(message) +sock.sendall(message) # Receive the message from the responder -received = sock.recv() +received = sock.recv(2048) # Feed the received message into noise payload = proto.read_message(received) # As of now, the handshake should be finished (as we are using NN pattern). # Any further calls to write_message or read_message would raise NoiseHandshakeError exception. # We can use encrypt/decrypt methods of NoiseBuilder now for encryption and decryption of messages. -encrypted_message = proto.encrypt('This is an example payload') +encrypted_message = proto.encrypt(b'This is an example payload') +sock.sendall(encrypted_message) -ciphertext = sock.recv() +ciphertext = sock.recv(2048) plaintext = proto.decrypt(ciphertext) +print(plaintext) +``` + +The example above covers the connection from the initiator's ("client") point of view. The snippet below is an example of responder's code ("server") using a socket connection to send and receive ciphertext. + +```python +import socket +from itertools import cycle + +from noise.builder import NoiseBuilder + +if __name__ == '__main__': + s = socket.socket() + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind(('localhost', 2000)) + s.listen(1) + + conn, addr = s.accept() + print('Accepted connection from', addr) + + noise = NoiseBuilder.from_name(b'Noise_NN_25519_ChaChaPoly_SHA256') + noise.set_as_responder() + noise.start_handshake() + + # Perform handshake. Break when finished + for action in cycle(['receive', 'send']): + if noise.handshake_finished: + break + elif action == 'send': + ciphertext = noise.write_message() + conn.sendall(ciphertext) + elif action == 'receive': + data = conn.recv(2048) + plaintext = noise.read_message(data) + + # Endless loop "echoing" received data + while True: + data = conn.recv(2048) + if not data: + break + received = noise.decrypt(data) + conn.sendall(noise.encrypt(received)) ``` #### Wireguard integration example