Module dryoc::classic::crypto_box

source ·
Expand description

Authenticated public-key cryptography functions

Implements libsodium’s public-key authenticated crypto boxes.

For details, refer to libsodium docs.

Classic API example

use dryoc::classic::crypto_box::*;
use dryoc::constants::CRYPTO_BOX_MACBYTES;
use dryoc::types::*;

// Create a random sender keypair
let (sender_pk, sender_sk) = crypto_box_keypair();

// Create a random recipient keypair
let (recipient_pk, recipient_sk) = crypto_box_keypair();

// Generate a random nonce
let nonce = Nonce::gen();

let message = "hello".as_bytes();
// Encrypt message
let mut ciphertext = vec![0u8; message.len() + CRYPTO_BOX_MACBYTES];
crypto_box_easy(&mut ciphertext, message, &nonce, &recipient_pk, &sender_sk)
    .expect("encrypt failed");

// Decrypt message
let mut decrypted_message = vec![0u8; ciphertext.len() - CRYPTO_BOX_MACBYTES];
crypto_box_open_easy(
    &mut decrypted_message,
    &ciphertext,
    &nonce,
    &sender_pk,
    &recipient_sk,
)
.expect("decrypt failed");

assert_eq!(message, decrypted_message);

Functions

Type Aliases

  • Crypto box message authentication code.
  • Nonce for crypto boxes.
  • Public key for public key authenticated crypto boxes.
  • Secret key for public key authenticated crypto boxes.