Crypt::NaCl::Sodium::scalarmult - Diffie-Hellman (Curve25519)
version 1.0.8.0
use Crypt::NaCl::Sodium qw( :utils ); my $crypto_scalarmult = Crypt::NaCl::Sodium->scalarmult(); my ($a_skey, $a_pkey, $a_shared_key, $a_key $b_skey, $b_pkey, $b_shared_key, $b_key); ## Alice ######## # Alice generates secret key $a_skey = $crypto_scalarmult->keygen(); # and computes the public key $a_pkey = $crypto_scalarmult->base( $a_skey ); # ... and shares it with Bob send_to( Bob => { public_key => $a_pkey } ); ## Bob ######## # Bob generates his secret key $b_skey = $crypto_scalarmult->keygen(); # and computes the public key $b_pkey = $crypto_scalarmult->base( $b_skey ); # ... and shares his public key with Alice send_to( Alice => { public_key => $b_pkey } ); # Bob receives the public key from Alice $a_key = receive_for( Bob => 'public_key' ); # Bob can now calculate the shared secret key $b_shared_key = $crypto_scalarmult->shared_secret( $b_skey, $a_key ); ## Alice ######## # Alice receives the public key from Bob $b_key = receive_for( Alice => 'public_key' ); # and can now also calculate the shared secret key $a_shared_key = $crypto_scalarmult->shared_secret( $a_skey, $b_key ); # shared keys calculated by Alice and Bob are equal $a_shared_key eq $b_shared_key or die; # NOTE: send_to() and receive_for() and user functions providing transport of # messages
crypto_scalarmult provides Curve25519, a state-of-the-art Diffie-Hellman function suitable for a wide variety of applications, eg. authenticate and encrypt messages between both parties.
crypto_scalarmult
my $secret_key = $crypto_scalarmult->keygen();
Helper method to generate a random secret key to be used by $crypto_scalarmult.
$crypto_scalarmult
The length of the $secret_key equals "SCALARBYTES".
$secret_key
The same secret key can be used with multiple other users, without the need of generating a distinct set of keys.
NOTE: keep the secret key confidential.
Returns Data::BytesLocker objects.
my $public_key = $crypto_scalarmult->base( $secret_key );
Computes the public key for corresponding secret key.
The length of the $public_key equals "BYTES".
$public_key
my $shared_key = $crypto_scalarmult->shared_secret( $sender_secret_key, $recipient_public_key );
Given a user's secret key $sender_secret_key and another user's public key $recipient_public_key, computes a secret shared by the two users and returns it. This secret can then be used to authenticate and encrypt messages between the two users.
$sender_secret_key
$recipient_public_key
The length of the $shared_key equals "BYTES".
$shared_key
BYTES and SCALARBYTES are provided for consistency, but it is safe to assume that BYTES == SCALARBYTES.
BYTES
SCALARBYTES
BYTES == SCALARBYTES
my $pkey_length = $crypto_scalarmult->BYTES;
Returns the length of public key (and shared secret).
my $skey_length = $crypto_scalarmult->SECRETKEYBYTES;
Returns the length of secret key.
crypto_scalarmult uses Curve25519.
Data::BytesLocker - guarded data storage
Crypt::Curve25519
Cryptography in NaCl
A state-of-the-art Diffie-Hellman function - Curve25519
Alex J. G. Burzyński <ajgb@cpan.org>
This software is copyright (c) 2015 by Alex J. G. Burzyński <ajgb@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Crypt::NaCl::Sodium, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Crypt::NaCl::Sodium
CPAN shell
perl -MCPAN -e shell install Crypt::NaCl::Sodium
For more information on module installation, please visit the detailed CPAN module installation guide.