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
- Computes a shared secret for the given
public_keyandprivate_key. Resulting shared secret can be used with the precalculation interface. - Detached variant of
crypto_box_easy. - Precalculation variant of
crypto_box_easy. - In-place variant of
crypto_box_detached_afternm. - In-place variant of
crypto_box_detached. - Encrypts
messagewith recipient’s public keyrecipient_public_key, sender’s secret keysender_secret_key, andnonce. The result is placed intociphertextwhich must be the length of the message plusCRYPTO_BOX_MACBYTESbytes, for the message tag. - Encrypts
messagewith recipient’s public keyrecipient_public_keyand sender’s secret keysender_secret_keyusingnoncein-place indata, without allocated additional memory for the message. - Generates a public/secret key pair using OS provided data using
rand_core::OsRng. - In-place variant of
crypto_box_keypair - Detached variant of
crypto_box_open_easy. - Precalculation variant of
crypto_box_open_easy. - In-place variant of
crypto_box_open_detached_afternm. - In-place variant of
crypto_box_open_detached. - Decrypts
ciphertextwith recipient’s secret keyrecipient_secret_keyand sender’s public keysender_public_keyusingnonce. - Decrypts
ciphertextwith recipient’s secret keyrecipient_secret_keyand sender’s public keysender_public_keywithnoncein-place indata, without allocated additional memory for the message. - Encrypts
messagewith recipient’s public keyrecipient_public_key, using an ephemeral keypair and nonce. The length ofciphertextmust be the length of the message plusCRYPTO_BOX_SEALBYTESbytes for the message tag and ephemeral public key. - Decrypts a sealed box from
ciphertextwith recipient’s secret keyrecipient_secret_key, placing the result intomessage. The nonce and public are derived fromciphertext.messagelength should be equal to the length ofciphertextminusCRYPTO_BOX_SEALBYTESbytes for the message tag and ephemeral public key. - Deterministically derives a keypair from
seed, which can be of arbitrary length. - In-place variant of
crypto_box_seed_keypair
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.