Function generateSharedSecret

Generate a shared secret to encrypt / decrypt a payload between two parties.

agora.crypto.ECC.Point generateSharedSecret (
  bool is_sender,
  agora.crypto.ECC.Scalar our_secret,
  agora.crypto.ECC.Point their_pubkey
) nothrow @trusted;

Parameters

NameDescription
is_sender the sender encrypts, therefore our_secret is the sender's ephemeral secret key. The receiver must set this to false to make sure internal hashing is consistent between sender & receiver
our_secret either the ephemeral secret key, or the receiver's secret key if is_sender is false
their_pubkey either the ephemeral public key, or the receiver's public key if is_sender is false

Returns

a shared secret which can be used for encryption

Example

Pair alice = Pair.random();
Pair bob = Pair.random();

Point secret1 = generateSharedSecret(true, alice.v, bob.V);
Point secret2 = generateSharedSecret(false, bob.v, alice.V);
assert(secret1 == secret2);

static struct S
{
    int x = 123;
}

S s;
const payload = s.serializeFull();

ubyte[crypto_secretbox_NONCEBYTES] nonce;
randombytes_buf(nonce.ptr, nonce.length);

auto ciphertext_len = crypto_secretbox_MACBYTES + payload.length;
ubyte[] ciphertext = new ubyte[](ciphertext_len);
if (crypto_secretbox_easy(ciphertext.ptr, payload.ptr, payload.length,
    nonce.ptr, secret1[].ptr) != 0)
    assert(0);

ubyte[] decrypted = new ubyte[](payload.length);
if (crypto_secretbox_open_easy(decrypted.ptr, ciphertext.ptr,
    ciphertext_len, nonce.ptr, secret1[].ptr) != 0)
    assert(0);

S deserialized = deserializeFull!S(decrypted);
assert(deserialized.x == s.x);