Module fire_crypto::cipher

source ·
Expand description

Contains structs used for encryption and decryption.

§Example

use fire_crypto::cipher::{Keypair, Nonce};

// Alice creates a key only she knows.
let alice_privkey = Keypair::new();
// Bob creates a key only he knows.
let bob_privkey = Keypair::new();

// Alice sends it's public key to bob.
let alice_pubkey = alice_privkey.public();
// Bob sends it's public to alice.
let bob_pubkey = bob_privkey.public();

// Alice creates a shared key from bob public key.
let alice_sharedkey = alice_privkey.diffie_hellman(&bob_pubkey);
// Bob creates a shared key from alice public key.
let bob_sharedkey = bob_privkey.diffie_hellman(&alice_pubkey);
assert_eq!(alice_sharedkey, bob_sharedkey);

// To finally create a key so they can talk securely
// alice or bob needs to send the other a random nonce.
let nonce = Nonce::new();

let mut alice_key = alice_sharedkey.to_key(nonce.clone());
let mut bob_key = bob_sharedkey.to_key(nonce);

// Both have the same key and can talk securely with each other.

let mut msg = *b"Hey Bob";
let mac = alice_key.encrypt(msg.as_mut());
assert_ne!(&msg, b"Hey Bob");
// The encrypted message and the mac can be sent to bob
// with an unsecure channel.
bob_key.decrypt(msg.as_mut(), &mac).expect("mac invalid");
assert_eq!(&msg, b"Hey Bob");
// Alice securely said hi to bob.

Structs§

  • A Keypair that can only be used once.
  • A Key that allows to encrypt and decrypt messages.
  • A Keypair that can be used multiple times.
  • A message authentication code.
  • Get’s returned as an error if the generated mac and the received MAC are not equal.
  • A Key that allows to encrypt and decrypt messages.
    Without having to borrow mutably.