Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lgh06/d8038ab080dae78790dd3af6d8330515 to your computer and use it in GitHub Desktop.
Save lgh06/d8038ab080dae78790dd3af6d8330515 to your computer and use it in GitHub Desktop.
Encrypting and decrypting a message using secret key aead xchacha20poly1305_ietf (libsodium js)
'use strict'
const sodium = require('libsodium-wrappers');
sodium.ready.then(function()
{
console.log('sodium ready');
//xchacha20ploy1305 ietf
//A key and a nonce are generated by the encrypting party. The decrypting
//party should use them for decryption
//encryption
let key3 = sodium.crypto_aead_xchacha20poly1305_ietf_keygen();
let nonce3 = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
let ciphertext3 = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
'secret message.',
null,
null,
nonce3,
key3
)
console.log('ciphertext')
console.log(ciphertext3)
//decryption.
let decryptedText3 = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
null,
ciphertext3,
null,
nonce3,
key3
)
console.log('decrypted ciphertext')
console.log(sodium.to_string(decryptedText3))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment