Skip to content

libsodium.PKI.SharedSecret.DeriveSharedSecret

Andrew Lambert edited this page Nov 26, 2022 · 13 revisions

libsodium.PKI.SharedSecret.DeriveSharedSecret

Method Signatures

  Shared Function DeriveSharedSecret(RecipientPublicKey As MemoryBlock, SenderPrivateKey As libsodium.PKI.EncryptionKey) As MemoryBlock

Parameters

Name Type Comment
RecipientPublicKey MemoryBlock The public half of the recipient's key pair..
SenderPrivateKey EncryptionKey The sender's key pair.

Return value

The shared secret data.

Remarks

WARNING: This is (probably) not the method you are looking for. You probably want SharedSecret.Constructor(RecipientPublicKey, SenderPrivateKey).

Computes a shared secret (NOT a key) given a SenderPrivateKey and RecipientPublicKey.

The return value represents the X coordinate of a point on the curve. As a result, the number of possible keys is limited to the group size (≈2252; smaller than the key space), and the key distribution is not uniform. In addition, different values of RecipientPublicKey and SenderPrivateKey may return the same X coordinate.

For this reason, instead of directly using the return value as a shared key, it is recommended to generate a hash of the return value concatenated with both users' public keys:

  Dim secret As MemoryBlock = libsodium.PKI.SharedSecret.DeriveSharedSecret(RecipientPublicKey, SenderPrivateKey)
  Dim key As String = libsodium.GenericHash(secret + RecipientPublicKey + SenderPublicKey)

Or just use the Constructor method which does this automatically:

  Dim key As New libsodium.PKI.SharedSecret(RecipientPublicKey, SenderPrivateKey)
Clone this wiki locally