Coder Social home page Coder Social logo

simpledoubleratchet's Introduction

SimpleDoubleRatchet

A simple to use, transport agnostic implementation of The Double Ratchet Algorithm created by Open Whisper Systems.

Demo

- Alice is the initial sender, Bob is the initial receiver.
- Alice starts the handshake which gets sent over the already connected transport layer.
- Bob must wait for a message from Alice before sending his first message.

Usage

1) Opening the channel

First setup some communication channel you want to secure.

Sender

TcpClient SenderTx = new TcpClient(ReceiverIpAddress, ReceiverRXPort);
TcpServer SenderRx = new TcpServer(SenderIpAddress, SenderTXPort);

Receiver

TcpClient ReceiverTx = new TcpClient(SenderIpAddress, ReceiverTXPort);
TcpServer ReceiverRx = new TcpServer(ReceiverIpAddress, SenderRXPort);

Then create an instance of DRChannel and connect it to the communication channel.

Sender

DRChannel SenderChannel = new DRChannel(); // or new DRChannel(true) to disable encryption for debugging content
SenderChannel.HandleTransportSend = (string packet) => 
{
    SenderTx.Write(packet);
};
SenderRx.OnPacket = (string packet) => 
{
    SenderChannel.HandleTransportReceive(packet);
};
bool success = SenderChannel.Open(true); // isSender = true, 30 second timeout

Receiver

DRChannel ReceiveChannel = new DRChannel(); 
ReceiveChannel.HandleTransportSend = (string packet) => 
{
    ReceiverTx.Write(packet);
};
ReceiverRx.OnPacket = (string packet) => 
{
    ReceiveChannel.HandleTransportReceive(packet);
};
bool success = ReceiverChannel.Open(false); // isSender = false

2) Using the channel synchronously

Once the channel is opened successfully, the Receiver should wait for the first message and the Sender can send the first message to start the Double Ratchet process. Once the first message is received, the channel is bi-directional.

Receiver

while(true) 
{
    string message = ReceiverChannel.WaitForResponse();
    if ( message == "ping" ) 
    {
        ReceiverChannel.Send("pong");
    }
    else if ( message == "pong");
    {
        ReceiverChannel.Send("ping");
    }
}

Sender

SenderChannel.Send("ping");
while(true) 
{
    string message = SenderChannel.WaitForResponse();
    if ( message == "ping" ) 
    {
        SenderChannel.Send("pong");
    }
    else if ( message == "pong");
    {
        SenderChannel.Send("ping");
    }
}

3) Using the channel asynchronously

Instead of waiting for the responses individually, you can also provide a delegate to handle the incomming messages

Receiver

ReceiverChannel.OnMessage = (string message) => 
{
    if ( message == "ping" ) 
    {
        ReceiverChannel.Send("pong");
    }
    else if ( message == "pong" ) 
    {
        ReceiverChannel.Send("ping");
    }
};

Sender

SenderChannel.OnMessage = (string message) =>
{
    if ( message == "ping" ) 
    {
        SenderChannel.Send("pong");
    }
    else if ( message == "pong" )
    {
        SenderChannel.Send("ping");
    }
};
SenderChannel.Send("ping");

simpledoubleratchet's People

Contributors

brandonestockcti avatar

Watchers

greg jaskiewicz avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.