Module dryoc::classic::crypto_sign
source · Expand description
§Public-key signatures
This module implements libsodium’s public-key signatures, based on Ed25519.
§Classic API example
use dryoc::classic::crypto_sign::*;
use dryoc::constants::CRYPTO_SIGN_BYTES;
// Generate a random signing keypair
let (public_key, secret_key) = crypto_sign_keypair();
let message = b"These violent delights have violent ends...";
// Signed message buffer needs to be correct length
let mut signed_message = vec![0u8; message.len() + CRYPTO_SIGN_BYTES];
// Sign the message, placing the result into `signed_message`
crypto_sign(&mut signed_message, message, &secret_key).expect("sign failed");
// Allocate a new buffer for opening the message
let mut opened_message = vec![0u8; message.len()];
// Open the signed message, verifying the signature
crypto_sign_open(&mut opened_message, &signed_message, &public_key).expect("verify failed");
assert_eq!(&opened_message, message);
// Create an invalid message
let mut invalid_signed_message = signed_message.clone();
invalid_signed_message[5] = !invalid_signed_message[5];
// An invalid message can't be verified
crypto_sign_open(&mut opened_message, &invalid_signed_message, &public_key)
.expect_err("open should not succeed");
§Classic API example, detached mode
use dryoc::classic::crypto_sign::*;
use dryoc::constants::CRYPTO_SIGN_BYTES;
// Generate a random signing keypair
let (public_key, secret_key) = crypto_sign_keypair();
let message = b"Brevity is the soul of wit.";
let mut signature = [0u8; CRYPTO_SIGN_BYTES];
// Sign our message
crypto_sign_detached(&mut signature, message, &secret_key).expect("sign failed");
// Verify the signature
crypto_sign_verify_detached(&signature, message, &public_key).expect("verify failed");
Re-exports§
Structs§
- State for incremental signing interface.
Functions§
- Signs
message
, placing the result intosigned_message
. The length ofsigned_message
should be the length of the message plusCRYPTO_SIGN_BYTES
. - Signs
message
, placing the signature intosignature
upon success. Detached variant ofcrypto_sign_open
. - Finalizes the incremental signature for
state
, usingsecret_key
, copying the result intosignature
upon success, and consuming the state. - Verifies the computed signature for
state
andpublic_key
matchessignature
, consuming the state. - Initializes the incremental signing interface.
- Randomly generates a new Ed25519
(PublicKey, SecretKey)
keypair that can be used for message signing. - In-place variant of
crypto_sign_keypair
. - Verifies the signature of
signed_message
, placing the result intomessage
. The length ofmessage
should be the length of the signed message minusCRYPTO_SIGN_BYTES
. - Returns a keypair derived from
seed
, which can be used for message signing. - In-place variant of
crypto_sign_seed_keypair
. - Updates the signature for
state
withmessage
. - Verifies that
signature
is a valid signature formessage
using the givenpublic_key
.