Updated README.md

This commit is contained in:
Hannes 2021-05-14 18:22:18 +02:00
parent c6f198536a
commit 290cbd207a

View file

@ -20,8 +20,8 @@ let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk); // Creating Bo
let mut alice_ratchet = Ratchet::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey let mut alice_ratchet = Ratchet::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey
let data = b"Hello World".to_vec(); // Data to be encrypted let data = b"Hello World".to_vec(); // Data to be encrypted
let (header, encrypted) = alice_ratchet.ratchet_encrypt(&data); // Encrypting message with Alice Ratchet (Alice always needs to send the first message) let (header, encrypted, nonce) = alice_ratchet.ratchet_encrypt(&data); // Encrypting message with Alice Ratchet (Alice always needs to send the first message)
let decrypted = bob_ratchet.ratchet_decrypt(&header, &encrypted); // Decrypt message with Bobs Ratchet let decrypted = bob_ratchet.ratchet_decrypt(&header, &encrypted, &nonce); // Decrypt message with Bobs Ratchet
assert_eq!(data, decrypted) assert_eq!(data, decrypted)
``` ```
@ -33,11 +33,11 @@ let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk); // Creating Bo
let mut alice_ratchet = Ratchet::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey let mut alice_ratchet = Ratchet::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey
let data = b"Hello World".to_vec(); // Data to be encrypted let data = b"Hello World".to_vec(); // Data to be encrypted
let (header1, encrypted1) = alice_ratchet.ratchet_encrypt(&data); // Lost message let (header1, encrypted1, nonce1) = alice_ratchet.ratchet_encrypt(&data); // Lost message
let (header2, encrypted2) = alice_ratchet.ratchet_encrypt(&data); // Successful message let (header2, encrypted2, nonce2) = alice_ratchet.ratchet_encrypt(&data); // Successful message
let decrypted2 = bob_ratchet.ratchet_decrypt(&header2, &encrypted2); // Decrypting second message first let decrypted2 = bob_ratchet.ratchet_decrypt(&header2, &encrypted2, &nonce2); // Decrypting second message first
let decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1); // Decrypting latter message let decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1, &nonce1); // Decrypting latter message
let comp = decrypted1 == data && decrypted2 == data; let comp = decrypted1 == data && decrypted2 == data;
assert!(comp); assert!(comp);
@ -52,7 +52,7 @@ let sk = [1; 32];
let (mut bob_ratchet, _) = Ratchet::init_bob(sk); let (mut bob_ratchet, _) = Ratchet::init_bob(sk);
let data = b"Hello World".to_vec(); let data = b"Hello World".to_vec();
let (_, _) = bob_ratchet.ratchet_encrypt(&data); let (_, _, _) = bob_ratchet.ratchet_encrypt(&data);
``` ```
### Encryption after recieving initial message ### Encryption after recieving initial message
@ -67,11 +67,11 @@ let mut alice_ratchet = Ratchet::init_alice(sk, public_key);
let data = b"Hello World".to_vec(); let data = b"Hello World".to_vec();
let (header1, encrypted1) = alice_ratchet.ratchet_encrypt(&data); let (header1, encrypted1, nonce1) = alice_ratchet.ratchet_encrypt(&data);
let _decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1); let _decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1, &nonce1);
let (header2, encrypted2) = bob_ratchet.ratchet_encrypt(&data); let (header2, encrypted2, nonce2) = bob_ratchet.ratchet_encrypt(&data);
let decrypted2 = alice_ratchet.ratchet_decrypt(&header2, &encrypted2); let decrypted2 = alice_ratchet.ratchet_decrypt(&header2, &encrypted2, &nonce2);
assert_eq!(data, decrypted2); assert_eq!(data, decrypted2);
``` ```
@ -83,6 +83,23 @@ let header_const = Header::from(header_bytes);
assert_eq!(header, header_const); assert_eq!(header, header_const);
``` ```
## Example Ratchet with encrypted headers
```rust
use double_ratchet_2::ratchet::RatchetEncHeader;
let sk = [0; 32];
let shared_hka = [1; 32];
let shared_nhkb = [2; 32];
let (mut bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
let mut alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
let data = b"Hello World".to_vec();
let (header, encrypted, nonce) = alice_ratchet.ratchet_encrypt(&data);
let decrypted = bob_ratchet.ratchet_decrypt(&header, &encrypted, &nonce);
assert_eq!(data, decrypted)
```
## Features ## Features
Currently the crate only supports one feature: ring. If feature is enabled the crate switches Currently the crate only supports one feature: ring. If feature is enabled the crate switches
@ -91,7 +108,7 @@ to ring-compat and uses ring as backend for Sha512 Hashing. May result in slight
TODO: TODO:
- [x] Standard Double Ratchet - [x] Standard Double Ratchet
- [ ] [Double Ratchet with encrypted headers][3] - [x] [Double Ratchet with encrypted headers][3]
[1]: https://signal.org/docs/specifications/doubleratchet/ [1]: https://signal.org/docs/specifications/doubleratchet/
[2]: https://signal.org/docs/specifications/doubleratchet/#recommended-cryptographic-algorithms [2]: https://signal.org/docs/specifications/doubleratchet/#recommended-cryptographic-algorithms