Skip to content

Instantly share code, notes, and snippets.

@842Mono
Created March 25, 2018 07:11
Show Gist options
  • Save 842Mono/2b3e4be6fb1a431f23942e6ed37efd57 to your computer and use it in GitHub Desktop.
Save 842Mono/2b3e4be6fb1a431f23942e6ed37efd57 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