See also: Libsodium's documentation on its public-key authenticated encryption features.
Public-key authenticated encryption.
Parameters and their respective types:
{string|Buffer}
plaintext{Buffer}
nonce (must be 24 bytes){X25519SecretKey}
secret key{X25519PublicKey}
public key
Returns a Promise
that resolves to a Buffer
.
Public-key authenticated encryption.
Parameters and their respective types:
{Buffer}
ciphertext{Buffer}
nonce (must be 24 bytes){X25519SecretKey}
secret key{X25519PublicKey}
public key
Returns a Promise
that resolves to a Buffer
.
Throws a SodiumError
on decryption failure.
Returns a Promise
that resolves to a CryptographyKey
containing a 64-byte
Buffer
. The first 32 bytes are your X25519 secret key, the latter 32 are your
X25519 public key.
Combine two X25519 keys (secret, public) into a keypair object.
Parameters and their respective types:
{X25519SecretKey}
secret key{X25519PublicKey}
public key
Returns a Promise
that resolves to a CryptographyKey
.
Parameters and their respective types:
{CryptographyKey}
(buffer must be 64 bytes long)
Returns a Promise
that resolves to a X25519PublicKey
.
Parameters and their respective types:
{CryptographyKey}
(buffer must be 64 bytes long)
Returns a Promise
that resolves to a X25519SecretKey
.
Derive the public key from a given X25519 secret key.
Parameters and their respective types:
{X25519SecretKey}
Returns a Promise
that resolves to a X25519PublicKey
.
const { SodiumPlus } = require('sodium-plus');
let sodium;
(async function () {
if (!sodium) sodium = await SodiumPlus.auto();
let aliceKeypair = await sodium.crypto_box_keypair();
let aliceSecret = await sodium.crypto_box_secretkey(aliceKeypair);
let alicePublic = await sodium.crypto_box_publickey(aliceKeypair);
let bobKeypair = await sodium.crypto_box_keypair();
let bobSecret = await sodium.crypto_box_secretkey(bobKeypair);
let bobPublic = await sodium.crypto_box_publickey(bobKeypair);
let plaintext = 'Your message goes here';
let nonce = await sodium.randombytes_buf(24);
let ciphertext = await sodium.crypto_box(plaintext, nonce, aliceSecret, bobPublic);
console.log(ciphertext);
let decrypted = await sodium.crypto_box_open(ciphertext, nonce, bobSecret, alicePublic);
console.log(decrypted.toString());
})();