[][src]Crate libp2p_secio

The secio protocol is a middleware that will encrypt and decrypt communications going through a socket (or anything that implements AsyncRead + AsyncWrite).

Connection upgrade

The SecioConfig struct implements the ConnectionUpgrade trait. You can apply it over a Transport by using the with_upgrade method. The returned object will also implement Transport and will automatically apply the secio protocol over any connection that is opened through it.

use futures::Future;
use libp2p_secio::{SecioConfig, SecioOutput};
use libp2p_core::{Multiaddr, identity, upgrade::apply_inbound};
use libp2p_core::transport::Transport;
use libp2p_tcp::TcpConfig;
use tokio_io::io::write_all;
use tokio::runtime::current_thread::Runtime;

let dialer = TcpConfig::new()
    .with_upgrade({
        // See the documentation of `identity::Keypair`.
        let keypair = identity::Keypair::rsa_from_pkcs8(private_key).unwrap();
        SecioConfig::new(keypair)
    })
    .map(|out: SecioOutput<_>, _| out.stream);

let future = dialer.dial("/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap())
    .unwrap()
    .map_err(|e| panic!("error: {:?}", e))
    .and_then(|connection| {
        // Sends "hello world" on the connection, will be encrypted.
        write_all(connection, "hello world")
    })
    .map_err(|e| panic!("error: {:?}", e));

let mut rt = Runtime::new().unwrap();
let _ = rt.block_on(future).unwrap();

Manual usage

Note: You are encouraged to use SecioConfig as described above.

You can add the secio layer over a socket by calling SecioMiddleware::handshake(). This method will perform a handshake with the host, and return a future that corresponds to the moment when the handshake succeeds or errored. On success, the future produces a SecioMiddleware that implements Sink and Stream and can be used to send packets of data.

Structs

SecioConfig

Implementation of the ConnectionUpgrade trait of libp2p_core. Automatically applies secio on any connection.

SecioMiddleware

Wraps around an object that implements AsyncRead and AsyncWrite.

SecioOutput

Output of the secio protocol.

Enums

Cipher

Possible encryption ciphers.

Digest

Possible digest algorithms.

KeyAgreement

Possible key agreement algorithms.

SecioError

Error at the SECIO layer communication.